我有一个包含下一个数据的集合:
db.MyCollection.insert({
id: 1,
Location: [ 1, 1 ],
Properties: [ { Type: 1, Value: "a" }, { Type: 2, Value: "b" }, { Type: 3, Value: "c" } ]
});
db.MyCollection.insert({
id: 2,
Location: [ 1, 2 ],
Properties: [ { Type: 1, Value: "a" }, { Type: 2, Value: "a" }, { Type: 3, Value: "c" } ]
});
db.MyCollection.insert({
id: 3,
Location: [ 2, 1 ],
Properties: [ { Type: 1, Value: "a" }, { Type: 3, Value: "b" }, { Type: 3, Value: "a" } ]
});
db.MyCollection.insert({
id: 4,
Location: [ 2, 2 ],
Properties: [ { Type: 2, Value: "b" }, { Type: 2, Value: "a" }, { Type: 3, Value: "c" } ]
});
db.MyCollection.ensureIndex({ Location: "2d"});
db.MyCollection.ensureIndex({ "Properties.Type": 1, "Properties.Value": 1});
db.MyCollection.ensureIndex({ Location: "2d", "Properties.Type": 1, "Properties.Value": 1});
我想要的是找到所有项目(使用上述任何索引):
- 匹配位置
- 包含 (Type=1 and Value="a") 和 (Type=2 and Value="b") 的属性
这是我的查询(它不起作用,但看起来接近正确的):
db.MyCollection.find(
{
Location: { "$within": { "$center": [ [1, 1], 5 ] } },
Properties: {
$elemMatch: {
$and: [
{ Type: 1, Value: "a" },
{ Type: 2, Value: "b" }
]
}
}
})
更新:
$all 查询效果更好,因为 $and 有问题(请参阅我在 JohnnyHK 答案中的评论)。感谢帮助。