How does mongodb insert subdocuments into the 2nd set one by one

  mongodb, question

Requirement: Query subdocuments and insert a new document item by item in combination with the parent document field

For example, a document:

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

Through sub-document splitting, expected results:

{
 "_id" : ObjectId("59bb5698c37fe6085b36f7d5"),
 "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("59bb5698c37fe6085b36f7d5"),
 "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"
 }

Is there any way to achieve this?

var a={
 "_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"
 }
 
 db.a.insert(a)
 
 db.a.aggregate(
 {
 $unwind:"$orderflowJsonEncode"
 },{
 $project:{
 _id:                    1,
 skuId:                  1,
 stockGroup:             1,
 calcDate:               "$orderflowJsonEncode.calcDate",
 acbQty:                 "$orderflowJsonEncode.acbQty",
 abcQty:                 "$orderflowJsonEncode.abcQty",
 stockQty:               "$orderflowJsonEncode.stockQty",
 isActiveDay:            "$orderflowJsonEncode.isActiveDay",
 outQtyWhenFullStockQty: "$orderflowJsonEncode.outQtyWhenFullStockQty",
 createdOn:              1,
 createdBy:              1
 }
 }
 ).pretty()

The operation results are as follows:

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

图片描述