简体   繁体   English

如何通过MongoDB中对象数组的多个条件过滤文档

[英]How to filter documents by multiple conditions on Array of Objects in MongoDB

Suppose, I have a collection in which each document looks like following:假设,我有一个集合,其中每个文档如下所示:

文档模式

Is it possible to find all documents of this collection based on following multiple conditions on Objects in modules array.是否可以根据模块数组中对象的以下多个条件找到该集合的所有文档。

{_id: OID123, status: available} and {_id: OID456, status: complete}

So that, it should return document similar to following:因此,它应该返回类似于以下的文档:

{modules: [{_id: OID123, status: available}, {_id: OID456, status: complete}, {_id: OID789, ...}] }

Schema/Configuration on Mongo playground for this problem Mongo 操场上针对此问题的架构/配置

Thank You everyone!谢谢大家!

I managed to solve this problem by following query using $all operator:我设法通过使用 $all 运算符进行查询来解决此问题:

db.collection.find({
  "modules": {
    $all: [
      {
        $elemMatch: {
          "_id": "OID1",
          "status": "available"
        }
      },
      {
        $elemMatch: {
          "_id": "OID2",
          "status": "complete"
        }
      }
    ]
  }
})

You can find more details on the mentioned playground link:您可以在上述游乐场链接上找到更多详细信息:

Mongo playground link with Query and example schema for this problem Mongo 游乐场链接与此问题的查询和示例模式

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

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