/// <summary> /// Returns true if query returns any document. This method does not deserialize any document. Needs indexes on query expression /// </summary> public bool Exists(Query query) { if (query == null) { throw new ArgumentNullException("query"); } var nodes = query.Run <T>(this); // if query execute with index, just returns nodes if (query.ExecuteMode == QueryExecuteMode.IndexSeek) { return(nodes.FirstOrDefault() != null); } // execute full scan foreach (var node in nodes) { var dataBlock = this.Database.Data.Read(node.DataBlock, true); var doc = BsonSerializer.Deserialize(dataBlock.Buffer).AsDocument; if (query.ExecuteFullScan(doc, new IndexOptions())) { return(true); } } return(false); }
/// <summary> /// Count documnets with a query. This method does not deserialize any document. Needs indexes on query expression /// </summary> public int Count(Query query) { if (query == null) { throw new ArgumentNullException("query"); } var nodes = query.Run <T>(this); // if query execute with index, just returns nodes if (query.ExecuteMode == QueryExecuteMode.IndexSeek) { return(nodes.Count()); } var count = 0; // execute full scan foreach (var node in nodes) { var dataBlock = this.Database.Data.Read(node.DataBlock, true); var doc = BsonSerializer.Deserialize(dataBlock.Buffer).AsDocument; if (query.ExecuteFullScan(doc)) { count++; } } return(count); }
internal override bool ExecuteFullScan(BsonDocument doc, IndexOptions options) { return(_left.ExecuteFullScan(doc, options) && _right.ExecuteFullScan(doc, options)); }
/// <summary> /// Find documents inside a collection using Query object. Must have indexes in query expression /// </summary> public IEnumerable <T> Find(Query query, int skip = 0, int limit = int.MaxValue) { if (query == null) { throw new ArgumentNullException("query"); } var nodes = query.Run <T>(this); // if query run on index, lets skip/take with linq-to-object if (query.ExecuteMode == QueryExecuteMode.IndexSeek) { if (skip > 0) { nodes = nodes.Skip(skip); } if (limit != int.MaxValue) { nodes = nodes.Take(limit); } } foreach (var node in nodes) { var dataBlock = this.Database.Data.Read(node.DataBlock, true); var doc = BsonSerializer.Deserialize(dataBlock.Buffer).AsDocument; // if need run in full scan, execute full scan and test return if (query.ExecuteMode == QueryExecuteMode.FullScan) { // execute query condition here - if false, do not add on final results if (query.ExecuteFullScan(doc, new IndexOptions()) == false) { continue; } // implement skip/limit before on full search - no linq if (--skip >= 0) { continue; } if (--limit <= -1) { yield break; } } // get object from BsonDocument var obj = this.Database.Mapper.ToObject <T>(doc); foreach (var action in _includes) { action(obj); } yield return(obj); } }
internal override bool ExecuteFullScan(BsonDocument doc) { return(_left.ExecuteFullScan(doc) && _right.ExecuteFullScan(doc)); }