1 module uim.mongo.classes.collection; 2 3 import uim.mongo; 4 5 class DMGOCollection { 6 this() {} 7 this(string aName) { this(); this.name(aName); } 8 this(MongoCollection aCollection) { this(); this.collection(aCollection); } 9 10 mixin(OProperty!("string", "name")); 11 mixin(OProperty!("MongoCollection", "collection")); 12 13 @safe: 14 /// Calculates aggregate values for the data in a collection. 15 Bson aggregate(ARGS...) (ARGS pipeline) { 16 return _collection.aggregate(pipeline); 17 } 18 unittest { 19 /* auto db = connectMongoDB("127.0.0.1").getDatabase("test"); 20 auto results = db["coll"].aggregate( 21 ["$match": ["status": "A"]], 22 ["$group": ["_id": Bson("$cust_id"), 23 "total": Bson(["$sum": Bson("$amount")])]], 24 ["$sort": ["total": -1]]); */ 25 } 26 27 MongoCursor!R aggregate(R, S) (S[] pipeline, AggregateOptions options) { 28 return _collection.aggregate(pipeline, options); 29 } 30 unittest { 31 /* 32 auto db = connectMongoDB("127.0.0.1").getDatabase("test"); 33 34 Bson[] args; 35 args ~= serializeToBson(["$match": ["status": "A"]]); 36 args ~= serializeToBson(["$group": ["_id": Bson("$cust_id"), 37 "total": Bson(["$sum": Bson("$amount")])]]); 38 args ~= serializeToBson(["$sort": ["total": -1]]); 39 40 AggregateOptions options; 41 options.cursor.batchSize = 10; // prefetch the first 10 results 42 auto results = db["coll"].aggregate(args, options); 43 */ 44 } 45 46 /// Counts the results of the specified query expression. 47 ulong count(T) (T query) { 48 return _collection.count(query); 49 } 50 51 /// Returns an input range of all unique values for a certain field for records matching the given query. 52 auto distinct(R, Q) (string key, Q query) { 53 return _collection.distinct(key, query); 54 } 55 unittest { 56 /* auto db = connectMongoDB("127.0.0.1").getDatabase("test"); 57 auto coll = db["collection"]; 58 59 coll.drop(); 60 coll.insert(["a": "first", "b": "foo"]); 61 coll.insert(["a": "first", "b": "bar"]); 62 coll.insert(["a": "first", "b": "bar"]); 63 coll.insert(["a": "second", "b": "baz"]); 64 coll.insert(["a": "second", "b": "bam"]); 65 66 auto result = coll.distinct!string("b", ["a": "first"]); 67 68 assert(result.equal(["foo", "bar"]));*/ 69 } 70 71 auto drop() { 72 _collection.drop(); 73 return null; 74 } 75 76 O dropIndex(this O)(string name) { 77 _collection.dropIndex(name); return cast(O)this; 78 } 79 80 /// Creates or updates an index. 81 O ensureIndex(O)(scope const(std.typecons.Tuple!(string,int))[] field_orders, IndexFlags flags = cast(IndexFlags)0, core.time.Duration expire_time = dur(0L)) { 82 _collection.ensureIndex(field_orders, flags, expire_time); return cast(O)this; 83 } 84 85 /// Queries the collection for existing documents. 86 MongoCursor!R find(R, T, U) (T query, U returnFieldSelector, QueryFlags flags = QueryFlags.None, int num_skip = 0, int num_docs_per_chunk = 0) { 87 return _collection.find(query, returnFieldSelector, flags, num_skip, num_docs_per_chunk); 88 } 89 90 MongoCursor!(T,R,typeof(null)) find(R, T) (T query) { 91 return _collection.find(query); 92 } 93 94 MongoCursor!(Bson,R,typeof(null)) find(R)() { 95 return _collection.find(); 96 } 97 98 /// Combines a modify and find operation to a single atomic operation. 99 Bson findAndModify(T, U, V) (T query, U update, V returnFieldSelector) { 100 return _collection.findAndModify(query, update, returnFieldSelector); 101 } 102 103 Bson findAndModify(T, U) (T query, U update) { 104 return _collection.findAndModify(query, update); 105 } 106 107 /// Combines a modify and find operation to a single atomic operation with generic options support. 108 Bson findAndModifyExt(T, U, V) (T query, U update, V options) { 109 return _collection.findAndModify(query, update, options); 110 } 111 112 /// Queries the collection for existing documents. 113 auto findOne(R, T, U)(T query, U returnFieldSelector, QueryFlags flags = QueryFlags.None) { 114 return _collection.findOne(query, returnFieldSelector, flags); 115 } 116 117 auto findOne(R, T) (T query) { 118 return _collection.findOne(query, returnFieldSelector, flags); 119 } 120 121 /// Inserts new documents into the collection. 122 O insert(this O, T)(T document_or_documents, InsertFlags flags = InsertFlags.None) { 123 _collection.insert(document_or_documents, flags); return cast(O)this; 124 } 125 126 /// Removes documents from the collection. 127 O remove(this O, T) (T selector, DeleteFlags flags = DeleteFlags.None) { 128 _collection.remove(selector, flags); return cast(O)this; 129 } 130 131 O remove(this O)() { 132 _collection.remove(); return cast(O)this; 133 } 134 135 /// Performs an update operation on documents matching 'selector', updating them with 'update'. 136 O update(this O, T, U) (T selector, U update, UpdateFlags flags = UpdateFlags.None) { 137 _collection.update(selector, update, flags); return cast(O)this; 138 } 139 } 140 auto MGOCollection() { return new DMGOCollection(); } 141 auto MGOCollection(string aName) { return new DMGOCollection(aName); } 142 auto MGOCollection(MongoCollection aCollection) { return new DMGOCollection(aCollection); }