更新mongo中的字段类型 [英] Update field type in mongo

查看:99
本文介绍了更新mongo中的字段类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在collection中有大量记录:

{field: [value]}

如何有效地更新为:

{字段:值}

我已经尝试过这样的事情:(pymongo语法)

I've tried something like this: (pymongo syntax)

collection.update({"field.1": {"$exists": True}},
                  {"$set": {'field': "field.1"}},
                  multi=True)

显然不起作用. 由于有大量的记录,因此无法循环遍历每个记录并删除插入.

which does not work apparently. Running through each record in a loop and removing-inserting is not an option because of the large number of records.

推荐答案

您需要遍历光标并使用$set update运算符更新每个文档.当然,您可以使用批量"操作来获得最大的效率.话虽这么说,但具体方法取决于您的MongoDB服务器版本和PyMongo版本.

You need to loop over the cursor and update each document using the $set update operator. Of course to do this you use "bulk" operations for maximum efficiency. That being said the approach will differ depending on your MongoDB server version and your PyMongo version.

从MongoDB 3.2开始,您需要使用批量写入操作 bulkWrite() 方法.

From MongoDB 3.2 you need to use Bulk Write Operations and the bulkWrite() method.

var requests = [];
var cursor = db.collection.find( { "field.1": { "$exists": true } }, { "field": 1 } );
cursor.forEach( document => { 
    requests.push({ 
        "updateOne": {
            "filter" : { "_id": document._id },
            "update" : { "field": { "$set": document.field[0] } }
        }
    });
    if (requests.length === 1000) {
        db.collection.bulkWrite(requests);
        requests = [];
    }
});

if (requests.length > 0) {
    db.collection.bulkWrite(requests);
}

此查询使用PyMongo 3.0驱动程序,该驱动程序提供您需要使用

This query using the PyMongo 3.0 driver which provides the you need to use the bulk_write() method gives the following:

from pymongo import UpdateOne


requests = [];
cursor = db.collection.find({"field.1": {"$exists": True}}, {"field": 1})
for document in cursor:
    requests.append(UpdateOne({'_id': document['_id']}, {'$set': {'field': document['field'][0]}}))
    if len(requests) == 1000:
        # Execute per 1000 operations
        db.collection.bulk_write(requests)
        requests = []
if len(requests) > 0:

    # clean up queues
    db.collection.bulk_write(requests)


从MongoDB 2.6开始,您需要使用现已弃用的批量 API.


From MongoDB 2.6 you need to use the now deprecated Bulk API.

var bulk = db.collection.initializeUnorderedBulkOp();
var count = 0;

// cursor is the same as in the previous version using MongoDB 3.2
cursor.forEach(function(document) { 
    bulk.find( { "_id": document._id } ).updateOne( { "$set": { "field": document.field[0] } } ); 
    count++;
    if (count % 1000 === 0) {
        bulk.execute();
        bulk = db.collection.initializedUnorderedBulkOp();
    }
});

// Again clean up queues
if (count > 0 ) {
    bulk.execute();
}

翻译成Python会得到以下结果.

Translate into Python gives the following.

bulk = db.collection.initialize_unordered_bulk_op()
count = 0

for doc in cursor:
    bulk.find({'_id': doc['_id']}).update_one({'$set': {'field': doc['field'][0]}})
    count = count + 1
    if count == 1000:
        bulk.execute()
        bulk = db.collection.initialize_unordered_bulk_op()

if count > 0:
    bulk.execute()

这篇关于更新mongo中的字段类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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