When does node-mongodb-native hits the database? -
i have problem understanding when database nail when using node-mongodb-native
. couldn't find reference on that. callback based, gave me feeling every single phone call hits database ... example, 2 snippets different in terms of how many times database nail :
// ---- 1 db.collection('bla', function(err, coll) { coll.findone({'blo': 'bli'}, function(err, doc) { coll.count(function(err, count) { console.log(doc, count) }) }) }) // ---- 2 db.collection('bla', function(err, coll) { coll.findone({'blo': 'bli'}, function(err, doc) { db.collection('bla', function(err, coll) { coll.count(function(err, count) { console.log(doc, count) }) }) }) })
i wondering whether can cache instances of collections , cursors. example, why not fetch collections need once, @ server start, , reuse same instances indefinitely ?
i'd understand how whole thing work, i'd appreciate link explaining stuff in details.
looking @ source code node.js driver collection
seems not ping mongodb upon creation of collection unless have strict mode on: https://github.com/mongodb/node-mongodb-native/blob/master/readme.md#strict-mode
the source code looked @ ( https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/db.js#l446 ) reinforced thought if strict not on seek , create new node.js collection object , run callback.
however findone
, count
break "lazy" querying of node.js , forcefulness query database in order results.
note: count
beingness on collection won't enforce "true" count of items in collection. instead garnish info collection meta.
so first snippet should see 2 queries run. 1 findone
, 1 count
, 2 sec snippet since creating collection after findone
should not enforce query mongodb.
mongodb node-mongodb-native
No comments:
Post a Comment