Pymongo,查询列表字段,和/或 [英] Pymongo, query on list field, and/or
本文介绍了Pymongo,查询列表字段,和/或的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含一些文档的集合,例如:
<代码>{_id: 5,值:[100, 1100, 1500]},{_id: 10,值:[1100, 1700]}
如何查询具有 vals
字段的文档:
- 1100
- 1700 或 100
- 100 和 1100
我可以使用一些理解魔法,例如:
g = lambda 代码: (d for d in collection.find() 如果有的话(d["vals"] 中的代码用于代码中的代码))g([100, 1700]).next()
或者,对于 AND:
g = lambda 代码: (d for d in collection.find() if all(code in d["vals"] for code in code))g([100, 1100]).next()
这看起来有点笨拙,但如果有一些可以通过驱动程序完成的查找魔法.
解决方案
yourmongocoll.find({"vals":1100})yourmongocoll.find({"$or":[ {"vals":1700}, {"vals":100}]})yourmongocoll.find({"$and":[ {"vals":100}, {"vals":1100}]})
我建议阅读 Mongodb 高级查询>
您还会找到 $in ...有用
I have a collection with some documents like:
{
_id: 5,
vals: [100, 1100, 1500]
},
{
_id: 10,
vals: [1100, 1700]
}
How can I query for documents that have, in vals
field:
- 1100
- 1700 OR 100
- 100 AND 1100
I can use some comprehension magic like:
g = lambda codes: (
d for d in collection.find() if any(code in d["vals"] for code in codes)
)
g([100, 1700]).next()
Or, for the AND:
g = lambda codes: (
d for d in collection.find() if all(code in d["vals"] for code in codes)
)
g([100, 1100]).next()
This seems a little clunky though if there is some find magic that can be done with the driver.
解决方案
yourmongocoll.find({"vals":1100})
yourmongocoll.find({"$or":[ {"vals":1700}, {"vals":100}]})
yourmongocoll.find({"$and":[ {"vals":100}, {"vals":1100}]})
i would recommend reading Mongodb Advanced queries
you will also find $in ...useful
这篇关于Pymongo,查询列表字段,和/或的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文