[英]Retrieve n-level deep sub-document in MongoDB
I have a deeply nested document in mongoDB and I would like to fetch individual sub-objects. 我在mongoDB中有一个深度嵌套的文档,我想获取单个子对象。
Example: 例:
{
"schoolName": "Cool School",
"principal": "Joe Banks",
"rooms": [
{
"number": 100
"teacher": "Alvin Melvin"
"students": [
{
"name": "Bort"
"currentGrade": "A"
},
// ... many more students
]
},
// ... many more rooms
]
}
Recently Mongo updated to allow 1-level-deep sub-object retrieval using $elemMatch projection : 最近Mongo更新为允许使用$ elemMatch投影进行1级深度子对象检索:
var projection = { _id: 0, rooms: { $elemMatch: { number: 100 } } };
db.schools.find({"schoolName": "Cool School"}, projection);
// returns { "rooms": [ /* array containing only the matching room */ ] }
But when I try to fetch a student (2 levels deep) in this same fashion, I get an error: 但当我尝试以同样的方式取一个学生(2级深)时,我收到一个错误:
var projection = { _id: 0, "rooms.students": { $elemMatch: { name: "Bort" } } };
db.schools.find({"schoolName": "Cool School"}, projection);
// "$err": "Cannot use $elemMatch projection on a nested field (currently unsupported).", "code": 16344
Is there a way to retrieve arbitrarily deep sub-objects in a mongoDB document? 有没有办法在mongoDB文档中检索任意深的子对象?
I am using Mongo 2.2.1 我正在使用Mongo 2.2.1
I recently asked a similar question and can provide a suitably general answer (see Using MongoDB's positional operator $ in a deeply nested document query ) 我最近问了一个类似的问题,并且可以提供一个适当的一般性答案(请参阅在深度嵌套的文档查询中使用MongoDB的位置运算符$ )
This solution is only supported for Mongo 2.6+, but from then you can use the aggregation framework's $redact function. 此解决方案仅支持Mongo 2.6+,但从那时起您可以使用聚合框架的$ redact函数。
Here is an example query which should return just your student Bort. 这是一个示例查询,它应该只返回您的学生Bort。
db.users.aggregate({
$match: { schoolName: 'Cool School' }
}, {
$project: {
_id: 0,
'schoolName': 1,
'rooms.number': 1,
'rooms.students': 1
}
}, {
$redact: {
$cond: {
"if": {
$or: [{
$gte: ['$schoolName', '']
}, {
$eq: ['$number', 100]
}]
},
"then": "$$DESCEND",
"else": {
$cond: {
"if": {
$eq: ['$name', 'Bort']
},
"then": "$$KEEP",
"else": "$$PRUNE"
}
}
}
}
});
$redact can be used to make sub-queries by matching or pruning sub-documents recursively in the matched documents. $ redact可用于通过在匹配文档中递归匹配或修剪子文档来进行子查询。
You can read about $redact here to understand more about what's going on but the design pattern I've identified has the following requirements: 您可以在此处阅读有关$ redact的内容,以了解有关正在发生的事情的更多信息,但我发现的设计模式具有以下要求:
$eq: ['$number', 100]
matches the room with number 100 $eq: ['$number', 100]
匹配数字为100的房间 $gte: ['$uniqueField': '']
$gte: ['$uniqueField': '']
匹配$gte: ['$uniqueField': '']
I don't have mongodb 2.2 handy at the moment, so I can't test this, but have you tried? 我目前没有mongodb 2.2方便,所以我无法测试这个,但你试过吗?
var projection = { _id: 0, rooms: { $elemMatch: { "students.name": "Bort" } } };
db.schools.find({"schoolName": "Cool School"}, projection);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.