[英]How to find all documents in a collection Mongodb by two values of the same key?
Tried to find the answer on the net, but somehow did not work.试图在网上找到答案,但不知何故没有奏效。 I have a collection from which I get a dataframe.我有一个集合,从中我得到一个 dataframe。 I need to filter, that is, get all documents in which the key value will match a certain list of values.我需要过滤,即获取其中键值将匹配某个值列表的所有文档。
Example collection:示例集合:
_id: '1'
key_1: 'a'
key_2 : 'b'
_id: '2'
key_1: 'a'
key_2 : 'c'
_id: '3'
key_1: 'd'
key_2 : 'e'
_id: '4'
key_1: 'c'
key_2 : 'f'
Based on this small example, I'm trying to display all documents where there will be values in key_1
= a
and d
.基于这个小例子,我试图显示key_1
= a
和d
中有值的所有文档。 That is, get documents with _id
1, 2 and 3 at the output.即在 output 处获取_id
为 1、2 和 3 的文档。
I use pomongo as well as compass, tried the following attempts but it was not successful:我使用 pomongo 和指南针,尝试了以下尝试但没有成功:
db.collection.find({'key_1': ['a', 'd']})
db.collection.find({'key_1': 'a', 'key_1': 'd'})
Tell me how can this be done?告诉我如何做到这一点?
Use $in
operator:使用$in
运算符:
db.collection.find( { key_1: { $in: [ 'a', 'd' ] } })
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.