在$ project中,我们可以替换_id并将_id值设置为数组中所有文档的1,2,3 ... n

时间:2018-02-21 15:11:34

标签: mongodb

在mongodb中使用项目,当我们将输出作为数组时,我们可以替换
_id并将_id值设为1,2,3 ... n
对于数组中的所有文档。 '项目'给出以下输出:
查询:

     db.premiumChange.aggregate([
     {'$unwind': '$history'}
     ,{'$match': {
       'history.startDate': {'$gte': ISODate('2018-01-21T11:13:14.000Z')}
       ,'history.endDate': {'$lte':ISODate('2018-02-20T11:13:14.000Z')}
     }}
     ,{'$facet':{'APTCChange':[{'$match': {'history.APTCChange': {'$gt':                          10}}}
     ,{'$limit':8}
     ,{ $project: {
     APTCChange: '$history.APTCChange'}}]
   ,"PremChange": [ {$match:{"history.APTCChange":{"$gt":10}}}
   ,{'$limit':1}
   ,{ $project: {
   APTCChange: '$history.APTCChange'}}]
 ,"MbrRespChg": [ {$match:{"history.MbrRespChg":{"$gt":10}}} 
 ,{'$limit':1}
 ,{ $project: {

 APTCChange: '$history.APTCChange'}}, {'$limit':1}] } }
 ])

上面的查询给出了以下结果,并自动在数组中添加了_id文档。 但根据我的要求,数组中的每个文档都应该_id为0,1,2..n。

    {
     "APTCChange" : [ 
    {
        "_id" : "HCCIDM1234567A",
        "APTCChange" : 1200,
    },
    {
        "_id" : "HCCIDM1234567B",
        "APTCChange" : 1200,
    }],

     "PremChange" : [ 
    {
        "_id" : "HCCIDM1234567A",
        "APTCChange" : 1200,
    },
    {
        "_id" : "HCCIDM1234567B",
        "APTCChange" : 1200,
    }],

     "MbrRespChg" : [ 
    {
        "_id" : "HCCIDM1234567A",
        "APTCChange" : 1200,
    },
    {
        "_id" : "HCCIDM1234567B",
        "APTCChange" : 1200,
    }]}

相反,我需要如下:

   {
   "APTCChange" : [ 
   {
        "_id" : 0,
        "APTCChange" : 1200,
   },
   {
        "_id" : 1,
        "APTCChange" : 1200,
   }],

   "PremChange" : [ 
   {
    "_id" : 3,
    "APTCChange" : 1200,
   },
   {
    "_id" : 4,
    "APTCChange" : 1200,
   }],

   "MbrRespChg" : [ 
   {
    "_id" : 5,
    "APTCChange" : 1200,
   },
   {
    "_id" : 6,
    "APTCChange" : 1200,
   }]}

1 个答案:

答案 0 :(得分:0)

尝试这种聚合,我们通过获取先前数组的$size与元素的当前数组索引相加来计算_id

管道

db.col.aggregate([
    {$project : {
        APTCChange : {
            $map : {
                input : "$APTCChange",
                as : "a",
                in : {
                    _id : {$indexOfArray : ["$APTCChange._id", "$$a._id"]},
                    APTCChange : "$$a.APTCChange"
                }
            }
        },
        PremChange : {
            $map : {
                input : "$PremChange",
                as : "a",
                in : {
                    _id : {$add : [{$indexOfArray : ["$PremChange._id", "$$a._id"]}, {$size : "$APTCChange"}]},
                    APTCChange : "$$a.APTCChange"
                }
            }
        },
        MbrRespChg : {
            $map : {
                input : "$MbrRespChg",
                as : "a",
                in : {
                    _id : {$add : [{$indexOfArray : ["$MbrRespChg._id", "$$a._id"]}, {$size : "$APTCChange"}, {$size : "$MbrRespChg"}]},
                    APTCChange : "$$a.APTCChange"
                }
            }
        }
    }}
]).pretty()

结果

{
    "_id" : ObjectId("5a9064a2996e36f33cd29dd0"),
    "APTCChange" : [
        {
            "_id" : 0,
            "APTCChange" : 1200
        },
        {
            "_id" : 1,
            "APTCChange" : 1200
        }
    ],
    "PremChange" : [
        {
            "_id" : 2,
            "APTCChange" : 1200
        },
        {
            "_id" : 3,
            "APTCChange" : 1200
        }
    ],
    "MbrRespChg" : [
        {
            "_id" : 4,
            "APTCChange" : 1200
        },
        {
            "_id" : 5,
            "APTCChange" : 1200
        }
    ]
}
>