您如何在mongodb中的数组上执行AND查询? [英] How do you do an AND query on an array in mongodb?

查看:58
本文介绍了您如何在mongodb中的数组上执行AND查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有标签的数组,该标签是文档的一部分,例如 [红色",绿色",蓝色",白色",黑色"]

I have an array with tags which is part of a document, eg ["red", "green", "blue", "white", "black"]

现在我要查找所有具有红色和蓝色的文档.

Now I want to find all documents, which have red AND blue.

推荐答案

使用$ all条件查找同时符合红色"和蓝色"条件的记录.

Use the $all condition to find records that match both "red" and "blue" conditions.

db.my_collection.find({tags: { $all : ["red","blue"]}})

如果要与红色"或蓝色"匹配的记录,则可以使用$ in条件.

If you want records that match either "red" or "blue" then you can use the $in condition.

db.my_collection.find({tags: { $in : ["red","blue"]}})

这篇关于您如何在mongodb中的数组上执行AND查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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