Update in forEach on mongodb shell

后端 未结 3 1270
天命终不由人
天命终不由人 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:42

    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

提交回复
热议问题