Mongodb concat没有聚合工作

时间:2016-09-27 06:54:02

标签: mongodb mongodb-query aggregation-framework mongodb-aggregation

在我的查询中,concat关键字无效,它返回null

这是查询: -

db.leads.aggregate([
{$project:{
    _id:0,
    status:1,
    stage:1,
    "todo.title":1,
    created:{
        day:{$substr:["$createdOn",8,2]},
        month:{$substr:["$createdOn",5,2]},
        year:{$substr:["$createdOn",0,4]}
        },
     myDate:{$concat:["$created.day","-","$created.month","-","$created.day"]}
         //----above $concat is not working-----//
         //--i want that `myDate` should be "12-09-2016"----//
    }
  }

])

这是查询输出: -

{
    "stage" : "Prospect",
    "todo" : [],
    "status" : "OPEN",
    "created" : {
        "day" : "12",
        "month" : "09",
        "year" : "2016"
    },
    "myDate" : null

   //--here i want that `myDate` should be "12-09-2016"----//
}
mongodb中的

createdOn 字段数据存储为日期类型,即

enter image description here

1 个答案:

答案 0 :(得分:4)

您不一定需要 $concat 运算符(即如果您使用的是MongoDB 3.0及更新版本), $dateToString 运算符这对你有好处:

db.leads.aggregate([
    {
        "$project": {           
            "status": 1,
            "stage": 1,
            "todo.title": 1,
            "created": {
                "day": { "$dayOfMonth": "$createdOn" },
                "month": { "$month": "$createdOn" },
                "year": { "$year": "$createdOn" }
            },
            "myDate": { "$dateToString": { "format": "%Y-%m-%d", "date": "$createdOn" } }   
        }
    }
])

如果您使用的MongoDB 2.6或更早版本不支持 $dateToString 运算符,那么您需要两个 $project 管道阶段。第一个创建创建的日期字段,然后将结果传递给创建myDate的下一个 $project 阶段。

以下示例显示了这种方法:

db.leads.aggregate([
    {
        "$project": {           
            "status": 1,
            "stage": 1,
            "todo.title": 1,
            "created": {
                "day": { "$substr": ["$createdOn", 8, 2] },
                "month": { "$substr": ["$createdOn", 5, 2] },
                "year": { "$substr": ["$createdOn", 0, 4] }
            }   
        }
    },
    {
        "$project": {
            "_id": 0,
            "status": 1,
            "stage": 1,
            "todo": 1,
            "created": 1,
            "myDate": { 
                "$concat": [
                    "$created.year", 
                    "-", 
                    "$created.month", 
                    "-", 
                    "$created.day"
                ] 
            }       
        }
    }
])

或者更确切地说是一个表达式为 $concat 参数的管道:

db.leads.aggregate([
    {
        "$project": {
            "_id": 0,           
            "status": 1,
            "stage": 1,
            "todo.title": 1,
            "created": {
                "day": { "$substr": ["$createdOn", 8, 2] },
                "month": { "$substr": ["$createdOn", 5, 2] },
                "year": { "$substr": ["$createdOn", 0, 4] }
            },
            "myDate": { 
                "$concat": [
                    { "$substr": ["$createdOn", 0, 4] }, 
                    "-", 
                    { "$substr": ["$createdOn", 5, 2] }, 
                    "-", 
                    { "$substr": ["$createdOn", 8, 2] }
                ] 
            }       
        }
    }
])