mongoskin和散装作业? (mongodb 3.2,mongoskin 2.1.0和2.2.0) [英] mongoskin and bulk operations? (mongodb 3.2, mongoskin 2.1.0 & 2.2.0)

查看:75
本文介绍了mongoskin和散装作业? (mongodb 3.2,mongoskin 2.1.0和2.2.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文学的各个方面,并且看到了与发问者相同的问题

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

https://stackoverflow.com/a/25636911

看到了.

我的代码如下:

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.insert可以正常工作

bulk.insert works just fine

bulk.execute未定义

bulk.execute is undefined

stackoverflow问题中的答案说:只有db.collection()的回调形式有效,所以我尝试了:

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

我们从不暗示"暗示db.collection()的回调风格"已删除3.0吗?

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

不幸的是,我的python比我的JS原型技术要好得多,因此查看皮肤源代码对我没有任何意义.

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.

使用mongoskin 2.1.0和2.2.0 mongodb JS驱动程序进行批量操作的正确方法是什么?还是不再实现呢?

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)使用插入,但使用数组形式,因此您只需一次调用即可插入多个文档.就像魅力一样.

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

(2)如果您确实需要批量操作,则需要从mongoskin切换到本地mongo接口,但仅适用于该调用. 有点烂,因为它在mongoskin中使用了专用接口,但这也是坚持使用mongoskin的最有效方法:

(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:

(coffeescript中的示例)

(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

或(3)如果要实现$ currentDate而不是任何泛型批量操作,请参考解决方案(1),但要使用没有很好说明的BSON对象Timestamp(),不带任何参数:

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)

将进行批量插入,并将时间戳记设置为将记录写入数据库时​​的DB服务器时间.

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.

这篇关于mongoskin和散装作业? (mongodb 3.2,mongoskin 2.1.0和2.2.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆