mongodb如何逐个插入子文档

时间:2017-09-18 02:05:55

标签: arrays mongodb mongodb-query

我在mongodb中有一个这样的文件:

db.full_stock_order_flow.find({skuId: “a19011100abc0084”,stockGroup: “ABC_01”})

结果:

{ 
    "_id" : ObjectId("59bb5698c37fe6085b36f7d5"), 
    "skuId" : "a19011100abc0084", 
    "stockGroup" : "ABC_01", 
    "orderflowJsonEncode" : [
        {
            "calcDate" : "2016-01-28", 
            "acbQty" : NumberInt(0), 
            "abcQty" : NumberInt(30), 
            "stockQty" : NumberInt(0), 
            "isActiveDay" : true, 
            "outQtyWhenFullStockQty" : NumberInt(30)
        }, 
        {
            "calcDate" : "2016-01-29", 
            "acbQty" : NumberInt(0), 
            "abcQty" : NumberInt(13), 
            "stockQty" : NumberInt(53), 
            "isActiveDay" : true, 
            "outQtyWhenFullStockQty" : NumberInt(13)
        }
    ], 
    "createdOn" : "2017-09-05 12:27:04", 
    "createdBy" : "helloworld"
}

我希望找到子文档并将其他collectin插入到父文档中,就像在另一个文档中没有子文档的更改2文档一样:

{ 
        "_id" : ObjectId("59bb5698c37kk6085b36f7d5"), 
        "skuId" : "a19011100abc0084", 
        "stockGroup" : "ABC_01", 
        "calcDate" : "2016-01-28", 
        "acbQty" : NumberInt(0), 
        "abcQty" : NumberInt(30), 
        "stockQty" : NumberInt(0), 
        "isActiveDay" : true, 
        "outQtyWhenFullStockQty" : NumberInt(30),
        "createdOn" : "2017-09-05 12:27:04", 
        "createdBy" : "helloworld"
    }


 { 
        "_id" : ObjectId("59bb5698c37kk6085b36f7d5"), 
        "skuId" : "a19011100abc0084", 
        "stockGroup" : "ABC_01", 
        "calcDate" : "2016-01-29", 
        "acbQty" : NumberInt(0), 
        "abcQty" : NumberInt(13), 
        "stockQty" : NumberInt(53), 
        "isActiveDay" : true, 
        "outQtyWhenFullStockQty" : NumberInt(13),
        "createdOn" : "2017-09-05 12:27:04", 
        "createdBy" : "helloworld"
    }

我该怎么办? 我用这个:

var record=db.full_stock_order_flow.find({skuId:"a19011100abc0084",stockGroup:"US_01"});
var arr=record.orderflowJsonEncode;
arr.forEach(
function(item){ 
   db.tt123.insert({"skuId": record.skuId,"stockGroup": record.stockGroup , "orderflowJsonEncode":item});
   } 
   )

0 个答案:

没有答案