在 MongoDB 中查找与对象数组中的多个字段匹配的文档 [英] Find documents matching multiple fields in an object array in MongoDB

查看:34
本文介绍了在 MongoDB 中查找与对象数组中的多个字段匹配的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个对象数组(让我们称之为数组 A),我需要一个查询来在 MongoDB 中找到一个集合,以查找与数组 A 中对象 1 的属性之一匹配的其中一个字段的所有文档,并且数组 A 中同一对象中某个其他属性的另一个字段.

Let's say I have an array of objects (let us call that array A) and I need a query to find a collection in MongoDB for all documents matching one of it's fields to one of the properties of object 1 in array A and another field to some other property in the same object in array A.

文档不具备数组 A 中的对象所具有的所有属性.

The documents do not have all the properties that the objects in array A have.

为了让事情清楚......

To make things clear...

数组 A 看起来像这样......

Array A would look something like this...

[{
    id_bus:1,
    id_bus_variation:13,
    ....
},{
    id_bus:2,
    id_bus_variation:184,
    ....
},{
    id_bus:3,
    id_bus_variation:13,
    ....
}]

我的数据库中的文档包含这两个属性,我需要同时匹配这两个属性.例如,我需要在我的数据库中找到具有 id_bus == 1id_bus_variation == 13 的文档,以及具有 id_bus == 的文档2id_bus_variation == 184 但不是 id_bus == 4id_bus_variation == 13 的那些.

The documents in my database include those two properties and I need to match those two at the same time. For example, I need to find in my database the docs that have id_bus == 1 and id_bus_variation == 13, and also the ones that have id_bus == 2 and id_bus_variation == 184 but not the ones that id_bus == 4 and id_bus_variation == 13.

我真的不知道如何使用单个查询来做到这一点,我发现的唯一解决方法是遍历数组 A 并为它的每个元素执行一个查询,匹配我需要的所有字段,但这似乎效率不高.

I really don't have any idea of how to do this using a single query, the only way around it I found is to go through array A and execute a query for each of it's elements, matching all the fields I need, but that doesn't seem efficient.

推荐答案

听起来您想将数组中子文档的结构与数组指定的许多可能结构之一进行匹配.我将举例说明如何在 mongo shell 中执行此操作:

It sounds like you want to match the structure of a subdocument in an array to one of many possible structures specified by an array. I'll give an example of how to do this in the mongo shell:

> db.test.insert({ 
    "_id" : 0, 
    bus : [
        { "id_bus" : 1, "id_bus_variation" : 1 },
        { "id_bus" : 2, "id_bus_variation" : 2 },
        { "id_bus" : 3, "id_bus_variation" : 3 }
    ]
})
> db.test.insert({ 
    "_id" : 1, 
    bus : [
        { "id_bus" : 1, "id_bus_variation" : 3 },
        { "id_bus" : 2, "id_bus_variation" : 2 },
        { "id_bus" : 3, "id_bus_variation" : 1 }
    ]
})
> db.test.insert({ 
    "_id" : 2, 
    bus : [
        { "id_bus" : 1, "id_bus_variation" : 1 },
        { "id_bus" : 2, "id_bus_variation" : 3 },
        { "id_bus" : 3, "id_bus_variation" : 2 }
    ]
})

如果我们要返回 (id_bus = 2 and id_bus_variation = 3) 或 (id_bus = 3 and 的所有文档id_bus_variation = 3),在数组中指定

If we want to return all documents where (id_bus = 2 and id_bus_variation = 3) or (id_bus = 3 and id_bus_variation = 3), as specified in an array

> var match = [{ "id_bus" : 2, "id_bus_variation" : 3 }, { "id_bus" : 3, "id_bus_variation" : 3 }];

我们可以通过编程方式构造查询:

We can construct the query programmatically:

> var query = { "$or" : [] }
> for (var i = 0; i < match.length; i++) {
    query["$or"].push({ "bus" : { "$elemMatch" : match[i] } });
}
> db.test.find(query, { "_id" : 1 }) // just get _id's for easy reading
{ "_id" : 0 }
{ "_id" : 2 }

我们得到了预期的结果.

We get the expected results.

这篇关于在 MongoDB 中查找与对象数组中的多个字段匹配的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆