简体   繁体   English

如何在文档中查找包含数组中所有元素的文档?

[英]how to find documents with all elements in the array in the document?

Hello I want to kno if is possible do a query for retrieve documents like this您好,我想知道是否可以查询这样的检索文档

Document文档

[
{
...
id:1
tags:['a', 'b', 'c']
...
},
{
...
id:2
tags:['b', 'c']
...
},
{
...
id:3
tags:['d']
...
}
]

findone({tags:{$all:['a', 'b']}}) -> 1 because a and b are only in doc 1 findone({tags:{$all:['b']}}) -> 1, 2 because b are only in doc 2 findone({tags:{$all:['a', 'b', 'd']}}) -> nothing, because any document has all tags inside array findone({tags:{$all:['d']}}) -> 3 because d are only in doc 3 findone({tags:{$all:['a', 'b']}}) -> 1 因为 a 和 b 只在 doc 1 findone({tags:{$all:['b']}}) -> 1, 2 因为findone({tags:{$all:['a', 'b', 'd']}})只在文档findone({tags:{$all:['d']}}) -> 3 因为 d 只在文档 3 中

There are any way of do this query?有什么办法可以做这个查询?

Thanks谢谢

Query询问

  • you can use $setDifference你可以使用$setDifference
  • if empty (setDifference your_array "$tags") => tags has all the elements, and filter is true if empty (setDifference your_array "$tags") => tags 具有所有元素,并且 filter 为真

Test code here测试代码在这里

aggregate(
[{"$match": 
    {"$expr": 
      {"$eq": [{"$setDifference": [["a", "b", "d"], "$tags"]}, []]}}}])

暂无
暂无

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

相关问题 Mongodb:查找具有数组的文档,其中所有元素都存在于查询数组中,但是文档数组可以更小 - Mongodb: find documents with array where all elements exist in query array, but document array can be smaller Mongoose - 如何在文档数组中查找文档 - Mongoose - How to find a Document inside an array of Documents 如何通过子文档数组中的数据查找文档 - How to find a document by data within an array of sub documents 如何在mongoose中更新所选文档中数组的所有嵌套文档? - How to update all nested documents of an array in a selected document in mongoose? 查找文档然后获取所有相关文档 - Find document then get all related documents 查找在MongoDB / Mongoose中给定数组中包含子集合的所有元素的特定字段的所有文档 - Find all documents where a particular field of all elements of a subcollection are included inside a given array in MongoDB / Mongoose 如果文档的 _id 已经存在,如何将数组元素推送到数组,如果 _id 不存在,如何创建新文档? - How to push array elements to an array if _id of documents already exists and create new document if _id doesn't exist? 如何在数据库中查找数组的所有元素 - How to find all elements of array in database 如何返回MongoDB文档中数组所有元素的属性? - How to return property of all elements of array inside MongoDB document? 如何在MongoDB中删除文档和所有嵌入式文档? - How to remove document and all embedded documents in MongoDB?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM