mongodb管道聚合管道替换值
如果我有文档,并且我正在聚合,并且我想细分一个字段,如:mongodb管道聚合管道替换值,mongodb,Mongodb,如果我有文档,并且我正在聚合,并且我想细分一个字段,如: {data: {'date_created': '2011-01-01', 'title': 'abc'}, 'owner': 'Jim'} {data: {'date_created': '2011-05-01', 'title': 'def'}, 'owner': 'Bob'} {data: {'date_created': '2011-03-01', 'title': 'ghi'}, 'owner': 'Jim'} {data: {
{data: {'date_created': '2011-01-01', 'title': 'abc'}, 'owner': 'Jim'}
{data: {'date_created': '2011-05-01', 'title': 'def'}, 'owner': 'Bob'}
{data: {'date_created': '2011-03-01', 'title': 'ghi'}, 'owner': 'Jim'}
{data: {'date_created': '2011-03-01', 'title': ''}, 'owner': 'Sam'}
我想进行聚合,这样我只能获取某个日期之前创建的标题或返回一个空列表,我将如何构建聚合管道
因此,期望的输出是:
{owner: "Jim", titles: ["abc", "def"],
owner: "Bob", titles: [],
owner: "Sam", titles: []}
我有一个聚合管道,类似于:
lookup => unwind =>
{'$match':
{'$or': [{'data.date_created': {'$lte': requested_date}}, {'data.title': {'$exists': False}}]}}}
<>但是我不知道如何在所需的时间是空白的情况下创建数据,所以它将与空白标题分组。 < P>如果没有严格的要求,当没有与您的条件匹配的项时,空数组会使它更简单。如果是这种情况,您只需使用
$push
或$addToSet
进行$match
和$group
例如:
db.foo.aggregate([
{ $match : {'data.date_created': {'$lte': "2011-03-01"}}},
{ $group: {
_id: "$owner",
titles : {$push : "$data.title"}
}}
])
db.foo.aggregate([
{ $group: {
_id: "$owner",
data : {$push : "$data"}
}},
{
$project: {
titles: {
$filter: {
input: "$data",
as: "item",
cond: { $lte: [ "$$item.date_created", "2011-03-01" ] }
}
}
}}
])
结果:
{ "_id" : "Sam", "titles" : [ "" ] }
{ "_id" : "Jim", "titles" : [ "abc", "ghi" ] }
{ "_id" : "Sam", "titles" : [ { "date_created" : "2011-03-01", "title" : "" } ] }
{ "_id" : "Bob", "titles" : [ ] }
{ "_id" : "Jim", "titles" : [ { "date_created" : "2011-01-01", "title" : "abc" }, { "date_created" : "2011-03-01", "title" : "ghi" } ] }
另一个选项是先执行$group/$push
,然后使用(需要MongoDB 3.2+)过滤数组元素
例如:
db.foo.aggregate([
{ $match : {'data.date_created': {'$lte': "2011-03-01"}}},
{ $group: {
_id: "$owner",
titles : {$push : "$data.title"}
}}
])
db.foo.aggregate([
{ $group: {
_id: "$owner",
data : {$push : "$data"}
}},
{
$project: {
titles: {
$filter: {
input: "$data",
as: "item",
cond: { $lte: [ "$$item.date_created", "2011-03-01" ] }
}
}
}}
])
结果:
{ "_id" : "Sam", "titles" : [ "" ] }
{ "_id" : "Jim", "titles" : [ "abc", "ghi" ] }
{ "_id" : "Sam", "titles" : [ { "date_created" : "2011-03-01", "title" : "" } ] }
{ "_id" : "Bob", "titles" : [ ] }
{ "_id" : "Jim", "titles" : [ { "date_created" : "2011-01-01", "title" : "abc" }, { "date_created" : "2011-03-01", "title" : "ghi" } ] }
如果在没有与您的条件匹配的项的情况下,您不严格要求空数组,这将使它变得更简单。如果是这种情况,您只需使用
$push
或$addToSet
进行$match
和$group
例如:
db.foo.aggregate([
{ $match : {'data.date_created': {'$lte': "2011-03-01"}}},
{ $group: {
_id: "$owner",
titles : {$push : "$data.title"}
}}
])
db.foo.aggregate([
{ $group: {
_id: "$owner",
data : {$push : "$data"}
}},
{
$project: {
titles: {
$filter: {
input: "$data",
as: "item",
cond: { $lte: [ "$$item.date_created", "2011-03-01" ] }
}
}
}}
])
结果:
{ "_id" : "Sam", "titles" : [ "" ] }
{ "_id" : "Jim", "titles" : [ "abc", "ghi" ] }
{ "_id" : "Sam", "titles" : [ { "date_created" : "2011-03-01", "title" : "" } ] }
{ "_id" : "Bob", "titles" : [ ] }
{ "_id" : "Jim", "titles" : [ { "date_created" : "2011-01-01", "title" : "abc" }, { "date_created" : "2011-03-01", "title" : "ghi" } ] }
另一个选项是先执行$group/$push
,然后使用(需要MongoDB 3.2+)过滤数组元素
例如:
db.foo.aggregate([
{ $match : {'data.date_created': {'$lte': "2011-03-01"}}},
{ $group: {
_id: "$owner",
titles : {$push : "$data.title"}
}}
])
db.foo.aggregate([
{ $group: {
_id: "$owner",
data : {$push : "$data"}
}},
{
$project: {
titles: {
$filter: {
input: "$data",
as: "item",
cond: { $lte: [ "$$item.date_created", "2011-03-01" ] }
}
}
}}
])
结果:
{ "_id" : "Sam", "titles" : [ "" ] }
{ "_id" : "Jim", "titles" : [ "abc", "ghi" ] }
{ "_id" : "Sam", "titles" : [ { "date_created" : "2011-03-01", "title" : "" } ] }
{ "_id" : "Bob", "titles" : [ ] }
{ "_id" : "Jim", "titles" : [ { "date_created" : "2011-01-01", "title" : "abc" }, { "date_created" : "2011-03-01", "title" : "ghi" } ] }
希望这有帮助。您可以在日期的基础上进行条件推送,然后从标题数组中筛选出空白项。
db.collection.aggregate([
{"$group":{
_id:"$owner",titles:{
$push: { $cond:[
{ $lte: [ "$data.date_created", "2011-03-01" ]},"$data.title",""
]
}
}
}
},
{
$project: {
_id:0,
owner : "$_id",
titles: {
$filter: {
input: "$titles",
as: "titles",
cond: { $ne: [ "$$titles", "" ] }
}
}
}
}
])
结果:
{ "titles" : [ ], "owner" : "Bob" }
{ "titles" : [ "abc", "ghi" ], "owner" : "Jim" }
{ "titles" : [ ], "owner" : "Sam" }
希望这有帮助。您可以在日期的基础上进行条件推送,然后从标题数组中筛选出空白项。
db.collection.aggregate([
{"$group":{
_id:"$owner",titles:{
$push: { $cond:[
{ $lte: [ "$data.date_created", "2011-03-01" ]},"$data.title",""
]
}
}
}
},
{
$project: {
_id:0,
owner : "$_id",
titles: {
$filter: {
input: "$titles",
as: "titles",
cond: { $ne: [ "$$titles", "" ] }
}
}
}
}
])
结果:
{ "titles" : [ ], "owner" : "Bob" }
{ "titles" : [ "abc", "ghi" ], "owner" : "Jim" }
{ "titles" : [ ], "owner" : "Sam" }
虽然不是标准的,但如果您愿意,可以通过使用虚拟字段(不存在的字段)来简化它,
$push
将吸收并不会添加任何内容<代码>{“$group”:{U id:“$owner”,标题:{$push:{$cond:[{$lte:[“$data.date\U created”,requested\U date]},“$data.title”,“$NONEEXIST”]}}}虽然不是标准的,但如果您愿意,可以使用虚拟字段(不存在的字段)来简化它其中,$push
将吸收并不会添加任何内容<代码>{“$group”:{U id:“$owner”,标题:{$push:{$cond:[{$lte:[“$data.date\U created”,请求的日期]},“$data.title”,“$NONEEXIST”]}}}