简体   繁体   English

如何过滤 mongodb 中另一个文档中的文档字段?

[英]how to filter the fields of a document within another document in mongodb?

My document is the following我的文件如下

{

 "name":"Name1",
  "status":"active",
  "points":[
   {
      "lag":"final"
   },
   {
      "lag":"final"
   }
   
  ]
},
{

 "name":"Name2",
 "status":"active",
  "points":[
   {
      "lag":"final"
   },
   {
      "lag":""
   }
   
  ]
}

I need to get all the documents that have some value in the lag field, for this example should get two document, I tried with this query, but it only returns me when all points have full lag我需要获取在滞后字段中具有某些值的所有文档,对于此示例应该获取两个文档,我尝试使用此查询,但它仅在所有点都具有完全滞后时才返回我

{ "points.lag":{$not:{ $eq:"" }},status:{$in:['active']}}

Play

You need to use elemMatch to check whether atleast one element matches the condition.您需要使用elemMatch来检查是否至少一个元素与条件匹配。

db.collection.find({
  "points": {
    "$elemMatch": {
      "lag": {
        $ne: null
      }
    }
  }
})

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

相关问题 集合的每个文档中的 MongoDB 过滤器数组字段 - MongoDB filter array fields within each document of a collection MongoDB文档中的过滤器数组 - Filter array within MongoDB document MongoDB使用Java,如何过滤同一文档的2个字段 - MongoDB Using Java , How do I filter on 2 fields of the same document MongoDB同一个文档中的两个字段如何比较过滤? - How can I compare and filter two fields in the same document in MongoDB? 如何在MongoDB中查询文档中的文档? - How to query a document within a document in MongoDB? MongoDb 过滤器以获取订单文档中的产品文档 - MongoDb Filter to Get Product Document within Order Document 有没有办法通过该文档中的其他值过滤 MongoDB 文档? - Is there a way to filter a MongoDB document by other values within that document? MongoDB $project 嵌入文档到根级别,其他字段在另一个文档中 - MongoDB $project embedded document to root level with other fields in another document 在MongoDB中,如何使用另一个文档(共享主键)中的其他字段更新文档? - In MongoDB, how do I update a document with the additional fields in another document (that shares a primary key)? 如何将文档中的字段和值复制到mongodb中不同集合中的另一个文档 - How to copy field and value within a document to another document in a different collection in mongodb
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM