如何使用mongoose从子文档数组中检索特定字段

时间:2022-11-11 21:26:24

I'm trying to get a specific field from a subdocument array

我正在尝试从子文档数组中获取特定字段

I'm not gonna include any of the fields in the parent doc

我不会在父文档中包含任何字段

Here is the sample document

这是示例文档

{
    "_id" : ObjectId("5409dd36b71997726532012d"),
    "hierarchies" : [ 
        {
            "rank" : 1,
            "_id" : ObjectId("5409df85b719977265320137"),
            "name" : "CTO",
            "userId" : [ 
                ObjectId("53a47a639c52c9d83a2d71db")
            ]
        }
    ]
}

I would like to return the rank of the hierarchy if the a userId is in the userId array

如果userId在userId数组中,我想返回层次结构的排名

here's what I have so far in my query

这是我在查询中到目前为止所拥有的内容

collectionName.find({{hierarchies:
   {$elemMatch : {userId: ObjectId("53a47a639c52c9d83a2d71db")}}}
    , "hierarchies.$.rank", function(err,data){}

so far it returns the entire object in the hierarchies array I want, but I would like to limit it to just the rank property of the object.

到目前为止,它返回我想要的层次结构数组中的整个对象,但我想将它限制为对象的rank属性。

1 个解决方案

#1


0  

The projection available to .find() queries generally in MongoDB does not do this sort of projection for internal elements of an array. All you can generally do is return the "matched" element of the array entirely.

通常在MongoDB中可用于.find()查询的投影不会对数组的内部元素执行此类投影。您通常可以做的就是完全返回数组的“匹配”元素。

For what you want, you use the aggregation framework instead, which gives you more control over matching and projection:

根据您的需要,您可以使用聚合框架,这样您就可以更好地控制匹配和投影:

Model.aggregate([
    { "$match": {
        "hierarchies.userId": ObjectId("53a47a639c52c9d83a2d71db")
    }},
    { "$unwind": "$hierarchies" },
    { "$match": {
        "hierarchies.userId": ObjectId("53a47a639c52c9d83a2d71db")
    }},
    { "$project": {
        "rank": "$hierarchies.rank"
    }}
],function(err,result) {

})

That basically matches the documents, filters the array content of the document to just the match and then projects only the required field.

这基本上匹配文档,将文档的数组内容过滤到匹配,然后只投影必需的字段。

#1


0  

The projection available to .find() queries generally in MongoDB does not do this sort of projection for internal elements of an array. All you can generally do is return the "matched" element of the array entirely.

通常在MongoDB中可用于.find()查询的投影不会对数组的内部元素执行此类投影。您通常可以做的就是完全返回数组的“匹配”元素。

For what you want, you use the aggregation framework instead, which gives you more control over matching and projection:

根据您的需要,您可以使用聚合框架,这样您就可以更好地控制匹配和投影:

Model.aggregate([
    { "$match": {
        "hierarchies.userId": ObjectId("53a47a639c52c9d83a2d71db")
    }},
    { "$unwind": "$hierarchies" },
    { "$match": {
        "hierarchies.userId": ObjectId("53a47a639c52c9d83a2d71db")
    }},
    { "$project": {
        "rank": "$hierarchies.rank"
    }}
],function(err,result) {

})

That basically matches the documents, filters the array content of the document to just the match and then projects only the required field.

这基本上匹配文档,将文档的数组内容过滤到匹配,然后只投影必需的字段。