简体   繁体   English

返回查找查询中的整个文档-Mongodb

[英]Returns the whole document in find query - Mongodb

Assume I have document as; 假设我的文档为;

{
  id: 1,
  name: "alex",
  adress: [
    {type: "home" , street: "flower"},
    {type: "business" , street: "falls"},
    {type: "xxx" , street: "xxx"},
  ]
}

I want to get home adress. 我想home In order to get this, I wrote the following query; 为了得到这个,我编写了以下查询;

db.collection.find({id: 1, "adress.type" : "home"})

As a result of this query it returned me; 查询结果返回了我;

{
  id: 1,
  name: "alex",
  adress: [
    {type: "home" , street: "flower"},
    {type: "business" , street: "falls"},
    {type: "xxx" , street: "xxx"},
  ]
}

However what I want is to get only home adress ; 但是,我要的只是获得home adress

{
  id: 1,
  name: "alex",
  adress: [
    {type: "home" , street: "flower"}
  ]
}

How can I narrow the result of this query? 如何缩小此查询的结果? How should I wrote the query in order to get only the home adress ? 我应该如何编写查询以仅获取home adress


edit after trying the answers from @SagarReddy and @suzo. 在尝试@SagarReddy和@suzo的答案后进行编辑。 Their answers are correct if I have one match. 如果我有一场比赛,他们的答案是正确的。

The answers are not applicable in the following case; 答案不适用于以下情况;

{
  id: 1,
  name: "alex",
  adress: [
    {type: "home" , street: "flower"},
    {type: "business" , street: "falls"},
    {type: "home" , street: "instinct"},
    {type: "xxx" , street: "xxx"},
  ]
}

db.collection.find({id: 1, "adress.type" : "home"}, {"adress.$":1}) returns; db.collection.find({id: 1, "adress.type" : "home"}, {"adress.$":1})返回;

{ 
    id: 1
    "adress" : [
        {
            "type" : "home", 
            "street" : "flower"
        }
    ]
}

which is not true. 这是不对的。 It gets only one match. 只有一场比赛。

What do you I expect is; 我期望什么?

{ 
"id" : 1, 
"name" : "alex", 
"adress" : [
    {
        "type" : "home", 
        "street" : "flower"
    }, 
    {
        "type" : "home", 
        "street" : "instinct"
    }
]

} }

So question is still valid. 因此问题仍然有效。 How can I get only the matched parts in mongodb find query? 如何仅在mongodb find查询中找到匹配的部分?

You can use positional $ operator to limit the contents of an array from the query results to contain only the first element matching the query document like below : 您可以使用位置$运算符将查询结果中的数组内容限制为仅包含与查询文档匹配的第一个元素,如下所示:

db.collection.find({id: 1, "adress.type" : "home"}, {"name":1, "address.$":1});

But use $ in the projection document of the find() method or the findOne() method when you only need one particular array element in selected documents. 但是,当您在选定文档中只需要一个特定的数组元素时,请在find()方法或findOne()方法的投影文档中使用$

To get all the matched elements in the array instead of the first element only, you can use $filter aggregator to do that like below : 要获取数组中所有匹配的元素,而不仅仅是第一个元素,可以使用$ filter aggregator进行如下操作:

This is New in Mongodb version 3.2. 这是Mongodb 3.2版的新功能。

[{
 "$match": {
   "address.type": "home"
 }
},
{
"$project": {
  "address": {
    "$filter": {
      "input": "$address",
      "as": "address",
      "cond": {
        "$eq": ["$$address.type", "home"]
      }
    }
  }
}
}]

try aggregation match and project the address. 尝试聚合匹配并投影地址。

 db.collection.aggregate([
                  { $match : {id: 1, "adress.type" : "home"}},
                  {$project : { adress: 1} } 
                   ]}.exec(function (err, doc) {
        if (err) callback(err);
        else callback(null, doc);
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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