[英]MongoDB Composite Key query with one field unknown
I have a database that has a unique combination of two fields (x and i) for every entry. 我有一个数据库,其中每个条目都有两个字段(x和i)的唯一组合。 So I have set the _id field to be {_id: {a: x, b: i}}. 因此,我将_id字段设置为{_id:{a:x,b:i}}。 Now I want to retrieve all values that have a certain value x but that have any value for i. 现在,我想检索所有具有特定值x但对于i具有任何值的值。
Example: 例:
{_id: {a: 1, b: 5}},
{_id: {a: 1, b: 3}},
{_id: {a: 2, b: 5}}
{_id: {a: 3, b: 3}}
Now I want to do something like: db.find({_id: {a: 1, b: { $exists: true}})
or even easier: db.find({_id: {a: 1})
that should return: 现在,我想做类似的事情: db.find({_id: {a: 1, b: { $exists: true}})
或更简单: db.find({_id: {a: 1})
应该返回:
{_id: {a: 1, b: 5}},
{_id: {a: 1, b: 3}}
Is there any way I could achieve this? 有什么办法可以实现? Or in other words can you query in any way on this composite primary key? 或者换句话说,您可以通过任何方式查询此复合主键吗? Currently I added the fields to the object itself but this is not really an optimal solution as my data set gets really large. 目前,我将字段添加到对象本身,但这并不是最佳解决方案,因为我的数据集变得非常大。
Edit: 编辑:
db.someCollection.find({"_id.a": 1, "_id.b": { $exists: true}})
Seems to be a solution, this is however just as slow as adding a as a field (not in the key) to the object. 似乎是一个解决方案,这是然而就像添加一个作为字段(而不是在键)到对象慢。 Is there a faster method? 有没有更快的方法?
你有尝试过吗?
db.someCollection.find({"_id.a": 1, "_id.b": { $exists: true}})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.