如何在mongodb上查找和替换唯一字段名称

时间:2016-06-14 17:51:38

标签: regex mongodb mongodb-query

我在mongodb有160万份文件:

      {
        "_id" : ObjectId("57580c3f7e1a1469e772345b"),
        "https://www.....com/vr/s1812227" : {
            "suitability" : "children welcome",
            "details" : {
                "lookingCount" : 0,
                "photoUrl" : "https://www.....com/vr/s1812227/....",
                "partner" : null,
                 .........
                }
                .........
          }
    }

    {
        "_id" : ObjectId("57580c3f7e1a1469e772346d"),
        "https://www.....com/vr/s1812358" : {
            "suitability" : "children welcome",
            "details" : {
                "lookingCount" : 0,
                "photoUrl" : "https://www.....com/vr/s1812358/....",
                "partner" : null,
                 .........
                }
                .........
          }
    }

{
        "_id" : ObjectId("57580c3f7e1a1469e772346d"),
        "https://www.....com/vr/s1812358/unite/125" : {
            "suitability" : "children welcome",
            "details" : {
                "lookingCount" : 0,
                "photoUrl" : "https://www.....com/vr/s1812358/....",
                "partner" : null,
                 .........
                }
                .........
          }
    }

我想这样:

{
        "_id" : ObjectId("57580c3f7e1a1469e772345b"),
        "products" : {
            "suitability" : "children welcome",
            "details" : {
                "lookingCount" : 0,
                "photoUrl" : "https://www.....com/vr/s1812227/....",
                "partner" : null,
                 .........
                }
                .........
          }
    }

编辑内容....感谢您的回答和提前的兴趣。

更新 我尝试使用此代码,但最多可将1200个文档插入到新集合中。我有150万份文件。

db.sourceColl.find().forEach(function(doc) {
    for (var k in doc) {
            if (k.match(/^https.*/) ) {
                db.sourceColl.find({ "_id": doc._id });
                db.getSiblingDB('targetdb')['targetColl'].insert({products: doc[k]});
            }
    }
});

在尝试此操作后,将20个文档插入新集合中。我很困惑。如何重命名和复制新集合的所有文档。 UPDATE2:我使用robomongo,我认为robomongo有限制。这段代码在mongo shell中没有问题。搜索,替换和复制新文件。

var bulk = db.sourceColl.initializeOrderedBulkOp();
var counter = 0;
db.sourceColl.find().forEach(function(doc) {
    for (var k in doc) {
            if (k.match(/^https.*/) ) {
                print(k)
                bulk.find({ "_id": doc._id });
                db.getSiblingDB('targetDB')['targetColl'].insert({products: doc[k]});
                counter++;
            }
    }
    if ( counter % 1000 == 0 ) {
        bulk.execute();
        bulk = db.sourceColl.initializeOrderedBulkOp();
    }

});
if ( counter % 1000 != 0 )
    bulk.execute();

1 个答案:

答案 0 :(得分:0)

我认为robomongo有限制。这段代码在mongo shell中工作正常。搜索,替换和复制新集合。

var bulk = db.sourceColl.initializeOrderedBulkOp();
var counter = 0;
db.sourceColl.find().forEach(function(doc) {
    for (var k in doc) {
            if (k.match(/^https.*/) ) {
                print(k)
                bulk.find({ "_id": doc._id });
                db.getSiblingDB('targetDB')['targetColl'].insert({products: doc[k]});
                counter++;
            }
    }
    if ( counter % 1000 == 0 ) {
        bulk.execute();
        bulk = db.sourceColl.initializeOrderedBulkOp();
    }

});
if ( counter % 1000 != 0 )
    bulk.execute();

我修改了这个答案https://stackoverflow.com/a/25204168/6446251