Update in forEach on mongodb shell

后端 未结 3 1243
天命终不由人
天命终不由人 2020-12-14 16:12

I have got a collection aTable with 2 records:

 {
    \"title\" : \"record 1\",
    \"fields\" : [ 
        {
            \"_id\" : 1,
              


        
3条回答
  •  醉梦人生
    2020-12-14 16:52

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

提交回复
热议问题