简体   繁体   English

我无法正确获得查询的答案 MongoDB - Find 方法

[英]I can't get the answer to the query correctly MongoDB - Find method

My JSON:我的JSON:

  { 
    "user_id" : "1000" ,
    "boxes" : [
        {
            "box_id" : "12345", 
            "box_name" : "Box 3", 
            "items" : [], 
            "visible" : false 
        },
        {
            "box_id" : "2000", 
            "box_name" : "Box 1", 
            "items" : [],
            "visible" : true 
        }, 
        {
            "box_id" : "3000", 
            "box_name" : "Box 2", 
            "items" : [], 
            "visible" : true 
        }
    ], 
    "user_name" : "Jimmy", 
}

I'm just trying to get which box belongs to user_id:"1000" , which one's visible: true and which one's box_id: "3000" .我只是想知道哪个框属于user_id:"1000" ,哪个是visible: true哪个是box_id: "3000" My query is:我的查询是:

db.getCollection("users")
   .find({user_id:"1000", 
          $and: [{"boxes.box_id": "3000"},{"boxes.visible": true}]}, 
          {"boxes.$":1})

But I'm always getting box_id:12345 one.但我总是得到 box_id:12345 一个。 I couldn't find anything about it.我找不到任何关于它的信息。 Thank you for answers.谢谢你的回答。

According to the documentation for the positional $ operator , you can't use the $ with multiple conditions.根据位置 $ 运算符的文档,您不能将 $ 与多个条件一起使用。 You have to use $elemMatch instead to work around the limitation.你必须使用$elemMatch来解决这个限制。

Array Field Limitations数组字段限制

MongoDB requires the following when dealing with projection over arrays: MongoDB 在处理数组上的投影时需要以下内容:

  • Only one positional $ operator may appear in the projection document.投影文档中只能出现一个位置 $ 运算符。
  • Only one array field, the one being limited with the $ projection operator, should appear in the query document.查询文档中只应出现一个数组字段,即受 $ 投影运算符限制的字段。 Additional array fields in the query document may lead to undefined behavior.查询文档中的其他数组字段可能会导致未定义的行为。
  • The query document should only contain a single condition on the array field being projected.查询文档应该只包含正在投影的数组字段上的单个条件。 Multiple conditions may override each other internally and lead to undefined behavior.多个条件可能会在内部相互覆盖并导致未定义的行为。

Under these requirements, the following query is incorrect:在这些要求下,以下查询是不正确的:

 db.collection.find( { <array>: <value>, <someOtherArray>: <value2> }, { "<array>.$": 1 } )

To specify criteria on multiple fields of documents inside that array, use the $elemMatch query operator.要在该数组内的多个文档字段上指定条件,请使用 $elemMatch 查询运算符。 The following query returns the first document inside a grades array that has a mean of greater than 70 and a grade of greater than 90.以下查询返回成绩数组中的第一个文档,该文档的平均值大于 70 且等级大于 90。

 db.students.find( { grades: { $elemMatch: { mean: { $gt: 70 }, grade: { $gt:90 } } } }, { "grades.$": 1 } )

You must use the $elemMatch operator if you need separate conditions for selecting documents and for choosing fields within those documents.如果您需要单独的条件来选择文档和选择这些文档中的字段,则必须使用 $elemMatch 运算符。

So I would try a query like:所以我会尝试这样的查询:

db.users.find({ user_id:"1000", boxes: { $elemMatch: { "box_id": "3000", "visible": true } } }, {"boxes.$":1});

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

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