1

i have a document in mongodb like this:

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

result:

{ 
    "_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"
}

and i want to find the sub document and insert it the other collectin with parent Document like change 2 document without sub document in the other document:

{ 
        "_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"
    }

what can i do? i use this :

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});
   } 
   )
supersujj
  • 11
  • 1
  • 4
  • The "error" of `undefined` is because `.find()` returns an "array" itself. If you expected a single "document" in response you instead use `.findOne()`, or actually iterate the cursor results returned where more than one document is expected.. It's not really an efficient way if your intent is to create a new collection from the array contents rather than embedding them. You can look at [How do I save the array I get by using $lookup on one collection in Mongodb?](https://stackoverflow.com/questions/15641492/mongodb-remove-object-from-array) as an example of doing that. – Neil Lunn Sep 18 '17 at 02:17
  • i use: db.full_stock_order_flow.aggregate( [ {$match: {skuId:"a14011100abc0084",stockGroup:"abc_01"}}, { $unwind : "$orderflowJsonEncode" } ]).forEach(function(doc){ db.tt123.insert(doc); }); but i juse insert the only the first one result into tt123? – supersujj Sep 18 '17 at 02:58
  • i use findone() but it can still issue arr is undefined – supersujj Sep 18 '17 at 03:05
  • [Edit your question](https://stackoverflow.com/posts/46270642/edit) instead of posting in comments and show the code you are actually using. Also note if you are trying to run this in the mongo shell or something else. Note that simply using `$unwind` is not good enough since `_id` will be the same for each document produced from the original source. Also refer to the original comment and "make your intentions clear" within the body of the question itself. – Neil Lunn Sep 18 '17 at 03:09
  • thanks for advise,i just update the question,and my mind is "find sub document and insert the other collection one by one with parent Document " – supersujj Sep 18 '17 at 03:21

0 Answers0