Update in forEach on mongodb shell

左心房为你撑大大i 提交于 2019-12-17 23:16:59

问题


I have got a collection aTable with 2 records:

 {
    "title" : "record 1",
    "fields" : [ 
        {
            "_id" : 1,
            "items" : [ 
                1
            ]
        },
        {
            "_id" : 2,
            "items" : [ 
                2,3,4
            ]
        },
        {
            "_id" : 3,
            "items" : [ 
                5
            ]
        }
    ]
},

{
        "title" : "record 2",
        "fields" : [ 
            {
                "_id" : 4,
                "items" : [ 
                    7,8,9,10
                ]
            },
            {
                "_id" : 5,
                "items" : [ 

                ]
            },
            {
                "_id" : 6,
                "items" : [ 
                    11,12
                ]
            }
        ]
    }

I want to update fields aTable.fields.items from

items : [ 11,12 ]

to

items : [ 
   {item: 11, key: 0},
   {item:12, key: 0}
]

I browse fields with forEach but I can't save it:

var t = db.aTable.find();

t.forEach(function( aRow ) {
    aRow.fields.forEach( function( aField ){
        aField.items.forEach( function( item ){
            var aNewItem = { item: parseInt(item), ref: 0 };
            db.aTable.update(item, {$set:aNewItem})
        } )
    } )
});

回答1:


To get what you want you will need a few things:

t.forEach(function( aRow ) {
    var newFields = [];
    aRow.fields.forEach( function( aField ){
        var newItems = [];
        aField.items.forEach( function( item ){
            var aNewItem = { item: parseInt(item), ref: 0 };
            newItems.push( aNewItem );
        } );
        newFields.push({ _id: aField._id, items: newItems });
    } )
    aTable.update(
        { _id: aRow._id }, 
        { "$set": { "fields": newFields } }
    );
});

So basically you need to "re-construct" your arrays before updating




回答2:


You can make changes directly in the whole object and then save it. Try the following snippet

db.aTable.find().forEach(function (itemWrapper){
    itemWrapper.fields.forEach(function(field){
        var items = field.items;
        var newItems = [];
        items.forEach(function(item){
          var t = {'item':item,'key':0}
          newItems.push(t);      
        })
        field.items = newItems;
    })
    db.aTable.save(itemWrapper)
})

What I am doing is iterating over all items and making a new array with {item : 1 , key:0} and then setting it back to items array in field object.

This is the output after update :

{
    "_id" : ObjectId("5332a192ece4ce8362c7a553"),
    "title" : "record 1",
    "fields" : [ 
        {
            "_id" : 1,
            "items" : [ 
                {
                    "item" : 1,
                    "key" : 0
                }
            ]
        }, 
        {
            "_id" : 2,
            "items" : [ 
                {
                    "item" : 2,
                    "key" : 0
                }, 
                {
                    "item" : 3,
                    "key" : 0
                }, 
                {
                    "item" : 4,
                    "key" : 0
                }
            ]
        }, 
        {
            "_id" : 3,
            "items" : [ 
                {
                    "item" : 5,
                    "key" : 0
                }
            ]
        }
    ]
}

/* 1 */
{
    "_id" : ObjectId("5332a192ece4ce8362c7a554"),
    "title" : "record 2",
    "fields" : [ 
        {
            "_id" : 4,
            "items" : [ 
                {
                    "item" : 7,
                    "key" : 0
                }, 
                {
                    "item" : 8,
                    "key" : 0
                }, 
                {
                    "item" : 9,
                    "key" : 0
                }, 
                {
                    "item" : 10,
                    "key" : 0
                }
            ]
        }, 
        {
            "_id" : 5,
            "items" : []
        }, 
        {
            "_id" : 6,
            "items" : [ 
                {
                    "item" : 11,
                    "key" : 0
                }, 
                {
                    "item" : 12,
                    "key" : 0
                }
            ]
        }
    ]
}



回答3:


Starting Mongo 4.2, db.collection.update() can accept an aggregation pipeline, finally allowing the update of a field based on its current value, and thus using a query instead of javascript:

// {
//   title: "record 1",
//   fields: [
//     { _id: 1, items: [1] },
//     { _id: 2, items: [2, 3, 4] },
//     { _id: 3, items: [5] }
//   ]
// }
db.collection.update(
  {},
  [{ $set: {
       fields: { $map: {
         input: "$fields",
         as: "x",
         in: {
           _id: "$$x._id",
           items: { $map: {
             input: "$$x.items",
             as: "y",
             in: { item: "$$y", key: 0 }
           }}
         }
       }}
  }}],
  { multi: true }
)
// {
//   title: "record 1",
//   fields: [
//     { _id: 1, items: [ { item: 1, key: 0 } ] },
//     { _id: 2, items: [ { item: 2, key: 0 }, { item: 3, key: 0 }, { item: 4, key: 0 } ] },
//     { _id: 3, items: [ { item: 5, key: 0 } ] }
//   ]
// }
  • The first part {} is the match query, filtering which documents to update (in this case all documents).

  • The second part [{ $set: { fields: { $map: { ... } } }] is the update aggregation pipeline (note the squared brackets signifying the use of an aggregation pipeline):

    • $set is a new aggregation operator which in this case replaces the field's value.
    • Then (if we go passed the boilerplate) we're simply $mapping the two nested arrays.
    • More specifically, the second map transformation replaces [1, 2] by [{ item: 1, key: 0 }, { item: 2, key: 0 }].
  • Don't forget { multi: true }, otherwise only the first matching document will be updated.


来源:https://stackoverflow.com/questions/22656517/update-in-foreach-on-mongodb-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!