mongoskin and bulk operations? (mongodb 3.2, mongoskin 2.1.0 & 2.2.0)

纵然是瞬间 提交于 2019-12-12 02:13:56

问题


I've read the various bits of literature, and I'm seeing the same problem that the questioner in

https://stackoverflow.com/a/25636911

was seeing.

My code looks like this:

coll = db.collection('foobar');
bulk = coll.initializeUnorderedBulkOp();

for entry in messages {
    bulk.insert(entry);
}

bulk.execute(function (err, result) {
   if (err) throw err
   inserted += result.nInserted
});

bulk is an object

bulk.insert works just fine

bulk.execute is undefined

The answer in the stackoverflow question said, "only the callback flavor of db.collection() works, so I tried:

db.collection('foobar', function (err, coll) {
   logger.debug "got here"
   if (err) throw err
   bulk = coll.initializeUnorderedBulkOp()
   ... same code as before

We never get to "got here" implying that the "callback flavor" of db.collection() was dropped for 3.0?

Unfortunately, my python is way better than my JS prototyping skills, so looking at the skin source code doesn't make any sense to me.

What is the right way, with mongoskin 2.1.0 and the 2.2.0 mongodb JS driver, to do a bulk operation, or is this not implemented at all anymore?


回答1:


There are at least two answers:

(1) Use insert, but the array form, so you insert multiple documents with one call. Works like a charm.

(2) If you really need bulk operations, you'll need to switch from mongoskin to the native mongo interface, but just for that one call. This kinda sucks because it's using a private interface in mongoskin, but it's also the most efficient way to stick with mongoskin:

(example in coffeescript)

// bulk write all the messages in "messages" to a collection
// and insert the server's current time in the recorded field of
// each message

// use the _native interface and wait for callback to get collection
db._native.collection collectionName, (err, collection) ->
    bulk = collection.initializeUnorderedBulkOp()
    for message in messages
        bulk.find
            _id: message._id
        .upsert().updateOne
           $set: message
           $currentDate:
               recorded: true
    bulk.execute (err, result) ->
        // ... error and result checking code

or (3) if you want to implement that $currentDate and not any generic bulk operation, refer to solution (1) but use the not-very-well-documented BSON object Timestamp() with no arguments:

for msg in messages:
    msg.recorded = Timestamp()
db.mycollection.insert(msg)

which will do a bulk insert and set timestamp to the DB server's time at the time the record is written to the db.



来源:https://stackoverflow.com/questions/38810387/mongoskin-and-bulk-operations-mongodb-3-2-mongoskin-2-1-0-2-2-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!