简体   繁体   English

Mongo 查询以查找任何字段元素与任何查询参数元素匹配的文档

[英]Mongo query to find documents where any of the field's elements match any of the query param elements

I've scoured the docs and google.我已经搜索了文档和谷歌。 Maybe I just need help formulating the question.也许我只是需要帮助来制定这个问题。

I have a field that stores an array of values.我有一个存储值数组的字段。 We'll call it "tags" for simplicity.为简单起见,我们将其称为“标签”。 I want to be able to search for all documents that match ANY of the multiple tags I search on.我希望能够搜索与我搜索的多个标签中的任何一个匹配的所有文档。 Put another way, I want to find all records that are tagged with at least one (not necessarily all) of these tags.换句话说,我想找到所有标记有至少一个(不一定是全部)这些标签的记录。 Here's an example.这是一个例子。

Documents :文件:

{name: "My Thing 1", tags: ["cool", "important", "anothertag"]},
{name: "My Thing 2", tags: ["cool"]},
{name: "My Thing 3", tags: ["cool", "important"]}
{name: "My Thing 4", tags: ["important", "anothertag"]}
{name: "My Thing 5", tags: []}
{name: "My Thing 6", tags: ["anothertag"]}

Query: ["important", "cool"]查询: ["important", "cool"]

Result: documents 1-4 (not 5-6)结果:文档 1-4(不是 5-6)

See comment by whoami.请参阅 whoami 的评论。 $in does the trick. $in 可以解决问题。

暂无
暂无

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

相关问题 Mongo - 数组查询,仅查找所有元素匹配的位置 - Mongo - Array query, only find where all elements match 查询匹配连续数组元素的文档 - Query for documents where match contiguous array elements Mongo查询以查找具有包含非空值的数组元素的文档,这些值都不匹配指定值 - Mongo query to find documents with array elements that contain non-null values none of which match the specified value MongoDB匹配包含数组字段的文档,其中包含与查询匹配的所有元素 - MongoDB match documents that contain an array field with ALL elements that match the query 如何查询 mongo 以查找包含来自另一个数组的任何元素的数组? - How to query mongo to find arrays that contain any of the elements from another array? 查询所有元素的字段值相同的子文档数组 - Query an array of sub-documents where a field's value is same for all elements 查询以找到所有数组属性元素都在数组中的文档 - Query to find documents where all array property elements are in array 是否有任何查询可以通过检查 mongoDb 中嵌套嵌入文档的字段来查找文档 - is there any query to find documents by checking a field of a nested embedded document in mongoDb mongodb查询查找数组中的对象与另一个数组字段的元素不匹配的位置 - mongodb query to find where objects in an array does not match elements of another array field Mongodb返回文档和匹配查询的数组元素 - Mongodb return documents AND the array elements that match query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM