如何对 Mongo 中的数组进行 $and 查询?
我在 mongo 中有一个集合,其中对象显示如下:
{ name:"Scott", bio:"Stumped", roles:["USR","ADM"] }
可能有更多角色。我想执行交集查询的任意组合,如下所示:
db.coll.find({$and:[{roles:"USR"},{roles:{$ne:"ADM"}}]})
有些查询可能都是角色 =,有些查询可能是所有角色 !=,有些查询可能像上面的示例一样混合。我在使用 $or 和 $nor 方面取得了一定程度的成功,但绝对没有任何查询可以理解与 $and 一起工作。我什至尝试利用 $where 和 $elemMatch 来模拟我想要的。我也确实在努力避免应用程序处理交集的多个查询。有想法吗?
I have a collection in mongo where objects appear as follows:
{ name:"Scott", bio:"Stumped", roles:["USR","ADM"] }
Many more roles are possible. I want to perform any combination of intersection queries, like so:
db.coll.find({$and:[{roles:"USR"},{roles:{$ne:"ADM"}}]})
Some queries may be all role =, some may be all roles !=, and some may be mixed as with the above example. I have had some measure of success with $or and $nor, but absolutely no query I can fathom works with $and. I have even tried leveraging $where and $elemMatch to emulate what I want. I am also really trying to avoid multiple queries w/ the application handling intersection. Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
找到答案:
感谢 Hohhi 带领我走上正确的道路!
Found the answer:
Thanks Hohhi for leading me down the right path!
我认为这会对你有帮助,至少它会返回你正在寻找的结果
I think this would help you, at least it returns the result you are looking for