简体   繁体   English

如何通过相同键的两个值查找集合 Mongodb 中的所有文档?

[英]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 = ad中有值的所有文档。 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.

相关问题 如何使用相同的键但值不同来更新集合中的所有文档? - How to update all documents in a collection with same key but different values? 如何使用MongoDB查找两个字段中具有相同值的所有文档? - How to find all documents having an identical values in two fields with MongoDB? 如何合并同一个 MongoDB 集合中的两个文档并且只有一个公共密钥? - How to merge two documents in the same MongoDB collection and have only one common key? 如何在MongoDB集合中的所有文档中对键的值求和 - How to sum the value of a key across all documents in a MongoDB collection 如何使用 Java 从 MongoDB 中的所有文档中获取相同键的值? - How do I get values of same key from all documents in MongoDB using Java? 合并MongoDB中同一集合中的两个文档 - Merge two documents in the same collection in MongoDB MongoDB:检查该集合中所有文档中的字段是否相同 - MongoDB: Check if a field is same in all the documents in that collection 如何在mongoDB中查找具有所有指定数组值的所有文档? - How to find all documents with all specified array values in it in mongoDB? 如何将集合的文档分组到以唯一字段值作为键和文档计数作为 mongodb 中的映射值的映射中? - How to group documents of a collection to a map with unique field values as key and count of documents as mapped value in mongodb? 如何找到两个LocalDateTime之间插入的所有MongoDB文档 - How find all MongoDB documents inserted between two LocalDateTime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM