简体   繁体   English

在MongoDB中检索n级深层子文档

[英]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的内容,以了解有关正在发生的事情的更多信息,但我发现的设计模式具有以下要求:

  • The redact condition is applied at each sub-document level so you need a unique field at each level eg you can't have number as a key on both rooms and students say 编辑条件适用于每个子文档级别,因此您需要在每个级别都有一个唯一的字段,例如,您不能将数字作为两个房间的关键字,并且学生说
  • It only works on data fields not array indices so if you want to know the returned position of a nested document ( for example to update it ) you need to include that and maintain it in your documents 它只适用于数据字段而非数组索引,因此如果您想知道嵌套文档的返回位置( 例如更新它 ),您需要包含它并将其保存在文档中
  • Each part of the $or statement in $redact should match the documents you want at a specific level $ redact中$或语句的每个部分都应该匹配特定级别的文档
  • Therefore each part of the $or statement needs to include a match to the unique field of the document at that level. 因此, $或语句的每个部分都需要包含与该级别文档的唯一字段的匹配。 For example, $eq: ['$number', 100] matches the room with number 100 例如, $eq: ['$number', 100]匹配数字为100的房间
  • If you aren't specifying a query at a level, you need to still include the unique field. 如果您未在某个级别指定查询,则仍需要包含唯一字段。 For example, if it is a string you can match it with $gte: ['$uniqueField': ''] 例如,如果它是一个字符串,你可以将它与$gte: ['$uniqueField': '']匹配$gte: ['$uniqueField': '']
  • The last document level goes in the second if expression so that all of that document is kept. 最后一个文档级别位于第二个 if表达式中,以便保留所有该文档。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM