Mongodb concat 在聚合中不起作用
Mongodb concat not working in aggregation
在我的查询中,concat 关键字不起作用,它 return 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"----//
}
createdOn 字段数据存储在 mongodb 中作为日期类型,即
您不一定需要 $concat
operator (i.e. if you're using MongoDB 3.0 and newer), the $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 或更早版本不支持创建 myDate
的 $dateToString
operator then you need two $project
pipeline stages. The first creates the created date field which then pipes the result to the next $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] }
]
}
}
}
])
在我的查询中,concat 关键字不起作用,它 return 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"----//
}
createdOn 字段数据存储在 mongodb 中作为日期类型,即
您不一定需要 $concat
operator (i.e. if you're using MongoDB 3.0 and newer), the $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 或更早版本不支持创建 myDate
的 $dateToString
operator then you need two $project
pipeline stages. The first creates the created date field which then pipes the result to the next $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] }
]
}
}
}
])