//INSERTvar r = yield db.collection('inserts').insertOne({a:1});
assert.equal(1, r.insertedCount);
var r = yield db.collection('inserts').insertMany([{a:2}, {a:3}]);
assert.equal(2, r.insertedCount);
//FIND -like read
var docs = yield col.find({a:1}).limit(2).toArray();
assert.equal(2, docs.length);
//option 2 Find
var cursor = col.find({a:1}).limit(2);
while(yield cursor.hasNext()) {
var doc = yield cursor.next();
console.dir(doc);
}
//UPDATING var col = db.collection('updates');
var r = yield col.insertMany([{a:1}, {a:2}, {a:2}]);
assert.equal(3, r.insertedCount);
var r = yield col.updateOne({a:1}, {$set: {b: 1}});
assert.equal(1, r.matchedCount);
assert.equal(1, r.modifiedCount);
var r = yield col.updateMany({a:2}, {$set: {b: 1}});
assert.equal(2, r.matchedCount);
assert.equal(2, r.modifiedCount);
//DELETING documents var col = db.collection('removes');
var r = yield col.insertMany([{a:1}, {a:2}, {a:2}]);
assert.equal(3, r.insertedCount);
// Remove a single
var r = yield col.deleteOne({a:1});
assert.equal(1, r.deletedCount);
// Delete multiple var r = yield col.deleteMany({a:2});
assert.equal(2, r.deletedCount);
//FIND AND UPDATE OR FIND AND DELETE
var r = yield col.findOneAndUpdate({a:1}, {$set: {b: 1}}, { returnOriginal: false , sort: [[a,1]] , upsert: true });
assert.equal(1, r.value.b);
var r = yield col.findOneAndDelete({a:2});
assert.ok(r.value.b == null);
// Close connection db.close(); |