简体   繁体   中英

How to find and replace unique field name on mongodb

I have 1.6 million documents in mongodb like this:

      {
        "_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,
                 .........
                }
                .........
          }
    }

I want like this:

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

Edit content.... Thanks for your answer and interest in advance.

UPDATE I'm trying this code but maximum 1200 documents insert to new collection. I have 1.5 million documents.

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]});
            }
    }
});

After I'm try this and insert 20 documents to new collection. I'm so confused. how to rename and copy new collection all documents. UPDATE2: I use robomongo and I think there are limits in robomongo. This code works without problem in mongo shell. search, replace and copy new document.

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();

I think there are limits in robomongo. This code works fine in mongo shell. search, replace and copy new collection.

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();

I have modified this answer https://stackoverflow.com/a/25204168/6446251

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM