MongoDB:多个$ elemMatch [英] MongoDB: multiple $elemMatch

查看:346
本文介绍了MongoDB:多个$ elemMatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的MongoDB文档的结构如下:

I have MongoDB documents structured like this:

{_id: ObjectId("53d760721423030c7e14266f"),
fruit: 'apple',
vitamins: [
    {
     _id: 1,
     name: 'B7',
     usefulness: 'useful',
     state: 'free',
    }
    {
     _id: 2,
     name: 'A1',
     usefulness: 'useful',
     state: 'free',
    }
    {
     _id: 3,
     name: 'A1',
     usefulness: 'useful',
     state: 'non_free',
    }
  ]
}
{_id: ObjectId("53d760721423030c7e142670"),
fruit: 'grape',
vitamins: [
    {
     _id: 4,
     name: 'B6',
     usefulness: 'useful',
     state: 'free',
    }
    {
     _id: 5,
     name: 'A1',
     usefulness: 'useful',
     state: 'non_free',
    }
    {
     _id: 6,
     name: 'Q5',
     usefulness: 'non_useful',
     state: 'non_free',
    }
  ]
}

我想查询并获得同时具有{name: 'A1', state: 'non_free'}{name: 'B7', state: 'free'}的所有水果. 在最坏的情况下,如果无法获取这些条目,并且如果存在pymongo的等效代码,我至少要计算这些条目,以了解如何编写这些条目.

I want to query and get all the fruits which have both {name: 'A1', state: 'non_free'} and {name: 'B7', state: 'free'}. In the worst case I want at least to count these entries if getting them is not possible and if the equivalent code exists for pymongo, to know how to write it.

对于给定的示例,我只想检索苹果(第一个)文档.

For the given example I want to retrieve only the apple (first) document.

如果我使用$elemMatch,则它仅适用于一种情况,不适用于两种情况.例如.如果我查询find({'vitamins': {'$elemMatch': {'name': 'A1', 'state': 'non_free'}, '$elemMatch': {'name': 'B7', 'state': 'free'}}}),它将使用{'name': 'B7', 'state': 'free'}检索所有水果.

If I use $elemMatch it works only for one condition, but not for both. E.g. if I query find({'vitamins': {'$elemMatch': {'name': 'A1', 'state': 'non_free'}, '$elemMatch': {'name': 'B7', 'state': 'free'}}}) it will retrieve all the fruits with {'name': 'B7', 'state': 'free'}.

推荐答案

在这种情况下,您可以使用 $and-运算符.

In this case you can use the $and-operator .

尝试此查询:

find({
    $and: [
         {'vitamins': {'$elemMatch': {'name': 'A1', 'state': 'non_free'} } },
         {'vitamins': {'$elemMatch': {'name': 'B7', 'state': 'free'} } }
    ]
});

解释为什么只收到与第二个条件匹配的结果:传递给MongoDB的每个{}内部的对象都是键/值对.每个对象只能存在一次.当您尝试为同一键两次分配一个值时,第二个分配将覆盖第一个.在您的情况下,您为同一对象中的键$elemMatch分配了两个不同的值,因此第一个被忽略.实际到达MongoDB的查询只是find({'vitamins': {'$elemMatch': {'name': 'B7', 'state': 'free'}}}).

To explain why you received only the result matching the second criteria: The objects inside each {} you pass to MongoDB are key/value pairs. Each key can only exist once per object. When you try to assign a value to the same key twice, the second assignment will override the first. In your case you assigned two different values to the key $elemMatch in the same object, so the first one was ignored. The query which actually arrived in MongoDB was just find({'vitamins': {'$elemMatch': {'name': 'B7', 'state': 'free'}}}).

每当需要将相同的运算符两次应用于相同的键时,都需要使用$or$and.

Whenever you need to apply the same operator to the same key twice, you need to use $or or $and.

这篇关于MongoDB:多个$ elemMatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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