1

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();
tabarly
  • 41
  • 6
  • 1
    You really don't want to have dynamic field's name in your collection. You should reconsider your schema design. – styvane Jun 14 '16 at 18:29
  • 2
    @user3100115 I think that's exactly what the OP is trying to fix. tabarly, it would be best if you added your attempt at a solution to your question. – JohnnyHK Jun 14 '16 at 19:09
  • Thank you for your comments. I think I found the solution http://stackoverflow.com/questions/25202418/mongodb-how-to-rename-a-field-using-regex?rq=1 I am trying. I'll update my question. – tabarly Jun 14 '16 at 19:29

1 Answers1

0

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

Community
  • 1
  • 1
tabarly
  • 41
  • 6