如何使用mongo聚合循环遍历数组并返回所需的文档?
我有一个查询,我需要通过ID查找文档,然后我只需要返回符合特定条件的数组内的项目。
在下面的代码中,您将看到我只需要通过ID“匹配”一个文档,然后我将“限制”设置为一个(应该只有一个)。这是我真的感到困惑...然后我“解开”片段字段,这是数组,在数组内是子文档。我需要通过其中的数组的最后一项过滤掉这些子文档,因此我“投射”assignments_array的“切片”。这是我只需要assignments_array
为EMPTY的文件,或者assignment_history.to
为null的文件
db.getCollection('territories').aggregate([
{
"$match": {
"congregation": ObjectId("5c68c706f1f52047f08862b3")
}
},
{
"$limit": 1
},
{
$unwind: {
path: "$fragments"
}
},
{
$project: {
"fragments.number": 1,
"fragments.assignment_history": {"$slice": ["$fragments.assignment_history", -1]}
}
}
这让我得到了这个结果......
{
"_id": ObjectId("5c68c706f1f52047f08862b6"),
"fragments": {
"number": 1,
"assignment_history": [{
"to": ObjectId("5c68c706f1f52047f08862b4"),
"on": 1550370567067
}]
}
}
{
"_id": ObjectId("5c68c706f1f52047f08862b6"),
"fragments": {
"number": 2,
"assignment_history": []
}
}
{
"_id": ObjectId("5c68c706f1f52047f08862b6"),
"fragments": {
"number": 3,
"assignment_history": [{
"to": null,
"on": 1550370567067
}]
}
}
我需要最终得到2个对象,其中assignment_history.to
为null,而assignment_history
没有项目/长度。
你可以在$match
之后再使用一个$unwind
条件
db.getCollection('territories').aggregate([
{ "$match": { "congregation": ObjectId("5c68c706f1f52047f08862b3") }},
{ "$limit": 1 },
{ "$unwind": { "path": "$fragments" }},
{ "$match": {
"$or": [
{ "fragments.assignment_history.to": null },
{ "fragments.assignment_history.to": { "$exists": false }}
]
}},
{ "$project": {
"fragments.number": 1,
"fragments.assignment_history": { "$slice": ["$fragments.assignment_history", -1] }
}}
])
如何使用mongo聚合循环遍历数组并返回所需的文档?
我有一个查询,我需要通过ID查找文档,然后我只需要返回符合特定条件的数组内的项目。
在下面的代码中,您将看到我只需要通过ID“匹配”一个文档,然后我将“限制”设置为一个(应该只有一个)。这是我真的感到困惑...然后我“解开”片段字段,这是数组,在数组内是子文档。我需要通过其中的数组的最后一项过滤掉这些子文档,因此我“投射”assignments_array的“切片”。这是我只需要assignments_array
为EMPTY的文件,或者assignment_history.to
为null的文件
db.getCollection('territories').aggregate([
{
"$match": {
"congregation": ObjectId("5c68c706f1f52047f08862b3")
}
},
{
"$limit": 1
},
{
$unwind: {
path: "$fragments"
}
},
{
$project: {
"fragments.number": 1,
"fragments.assignment_history": {"$slice": ["$fragments.assignment_history", -1]}
}
}
这让我得到了这个结果......
{
"_id": ObjectId("5c68c706f1f52047f08862b6"),
"fragments": {
"number": 1,
"assignment_history": [{
"to": ObjectId("5c68c706f1f52047f08862b4"),
"on": 1550370567067
}]
}
}
{
"_id": ObjectId("5c68c706f1f52047f08862b6"),
"fragments": {
"number": 2,
"assignment_history": []
}
}
{
"_id": ObjectId("5c68c706f1f52047f08862b6"),
"fragments": {
"number": 3,
"assignment_history": [{
"to": null,
"on": 1550370567067
}]
}
}
我需要最终得到2个对象,其中assignment_history.to
为null,而assignment_history
没有项目/长度。
你可以在$match
之后再使用一个$unwind
条件
db.getCollection('territories').aggregate([
{ "$match": { "congregation": ObjectId("5c68c706f1f52047f08862b3") }},
{ "$limit": 1 },
{ "$unwind": { "path": "$fragments" }},
{ "$match": {
"$or": [
{ "fragments.assignment_history.to": null },
{ "fragments.assignment_history.to": { "$exists": false }}
]
}},
{ "$project": {
"fragments.number": 1,
"fragments.assignment_history": { "$slice": ["$fragments.assignment_history", -1] }
}}
])