public object FindAndRemoveAs(Type documentType, IMongoQuery query, IMongoSortBy sortBy) { foreach (var document in QueryCompiler.Query(Documents, Documents2, query, sortBy, 0, 0)) { RemoveDocument(document); return(BsonSerializer.Deserialize(document, documentType)); } return(null); }
public IEnumerable <object> FindAs(Type documentType, IMongoQuery query, QueryFlags modes, IMongoSortBy sortBy, int skip, int first, IMongoFields fields) { var iter = QueryCompiler.Query(Documents, Documents2, query, sortBy, skip, first); if (fields == null) { return(iter.Select(x => BsonSerializer.Deserialize(x, documentType))); } var project = FieldCompiler.GetFunction(fields); return(iter.Select(project).Select(x => BsonSerializer.Deserialize(x, documentType))); }
public object FindAndModifyAs(Type documentType, IMongoQuery query, IMongoSortBy sortBy, IMongoUpdate update, IMongoFields fields, bool returnNew, bool upsert, out UpdateResult result) { foreach (var document in QueryCompiler.Query(Documents, Documents2, query, sortBy, 0, 0)) { // if old is needed then deep(!) clone before update //_131103_185751 BsonDocument output = null; if (!returnNew) { output = document.DeepClone().AsBsonDocument; } UpdateDocument(document, UpdateCompiler.GetFunction((IConvertibleToBsonDocument)update, null, false)); if (returnNew) { output = document; } // project if (fields != null) { var project = FieldCompiler.GetFunction(fields); output = project(output); } // if old is needed then return it as already deep cloned result = new SimpleUpdateResult(1, true); if (!returnNew && documentType == typeof(Dictionary)) { return(new Dictionary(output)); } else { // deserialize to required type return(BsonSerializer.Deserialize(output, documentType)); } } // not found, insert if (upsert) { var document = InsertNewDocument(query, update); result = new SimpleUpdateResult(1, false); return(returnNew ? BsonSerializer.Deserialize(document, documentType) : null); } result = new SimpleUpdateResult(0, false); return(null); }
static Expression PushEachExpression(Expression that, Expression field, BsonDocument document) { int position = -1; BsonArray each = null; BsonValue sort = null, slice = null; foreach (var e in document.Elements) { //TODO switch (e.Name) { case "$each": if (e.Value.BsonType != BsonType.Array) { throw new ArgumentException("Push all/each value must be array."); } each = e.Value.AsBsonArray; break; case "$sort": sort = e.Value; break; case "$slice": if (!e.Value.IsNumeric) { throw new ArgumentException("$slice must be a numeric value."); } slice = e.Value; break; case "$position": if (!e.Value.IsNumeric) { throw new ArgumentException("$position must be a numeric value."); } position = e.Value.ToInt32(); if (position < 0) { throw new ArgumentException("$position must not be negative."); } break; default: throw new ArgumentException(string.Format(null, "Unrecognized clause in $push: ({0}).", e.Name)); } } if (sort != null) { switch (sort.BsonType) { case BsonType.Int32: switch (sort.AsInt32) { case 1: each = new BsonArray(each.OrderBy(x => x)); break; //TODO comparer? case -1: each = new BsonArray(each.OrderByDescending(x => x)); break; //TODO comparer? default: throw new ArgumentException("Numeric $sort value must be either 1 or -1."); } break; case BsonType.Document: //TODO not needed in Mongo v2.6 foreach (var d1 in each) { if (d1.BsonType != BsonType.Document) { throw new ArgumentException("$sort requires $each to be an array of objects."); } } each = new BsonArray(QueryCompiler.Query(each.Cast <BsonDocument>(), null, null, new SortByDocument(sort.AsBsonDocument), 0, 0)); break; default: throw new ArgumentException("$sort value must be 1, -1, or a document."); } } if (slice != null) { each = each.Slice(0, slice.ToInt32()); } return(Expression.Call(that, GetMethod("PushAll"), Data, field, Expression.Constant(each, typeof(BsonValue)), Expression.Constant(position, typeof(int)))); }