Created
May 13, 2019 20:33
-
-
Save luisenriquecorona/3b07789cd9d67ceab4a6fa992d873dde to your computer and use it in GitHub Desktop.
Let’s have a look at how actual filters that match only some of our documents look like. We are going to rewrite our script because we need some more documents to play with
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var MongoClient = require('mongodb').MongoClient; | |
MongoClient.connect( 'mongodb://127.0.0.1:27017/accounting', function (err, connection) { | |
var collection = connection.collection('customers'); | |
var doFind = function (callback) { collection.find().toArray(function (err, documents) { | |
console.dir(documents); | |
callback(); | |
}); | |
}; | |
var doInsert = function (i) { if (i < 20) { | |
var value = Math.floor(Math.random() * 10); collection.insert( | |
{'n': '#' + i, 'v': value}, function (err, count) { | |
doInsert(i + 1); | |
}); | |
} else { | |
console.log(); | |
console.log('Inserted', i, 'documents:'); doFind(function () { | |
doUpdate(); | |
}); | |
} }; | |
var doUpdate = function () { | |
collection.update( | |
{'v': {'$gt': 5}}, | |
{'$set': {'valuable': true}}, {'multi': true}, | |
function (err, count) { | |
console.log(); | |
console.log('Updated', count, 'documents:'); | |
doFind(function () { | |
collection.remove({}, function () { connection.close(); | |
}); | |
}); | |
}); | |
}; | |
doInsert(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment