我有一个产品架构
{
type:ObjectId,
point: { type:"Point", coordinates:Array},
category_id:ObjectId,
price:Number
}
我想查询过滤器类型,点,category_id,价格,点等:
Product.find({
type_id:{$in:[ObjectId("5d679819325ab70ab0157ce5"),...]},
category_id:{$in:[ObjectId("5d679819325ab70ab0157ce5"),...],}
price:{$gte:30},
point: {
$geoWithin: {
$box:boxInput: //like [ [ 10.479445601358, 10.479445601358 ], [ 105.67003470372651, 105.67003470372651] ]
}
}
})
boxInput带有任何查询的更改,问题是如何使用该查询创建索引?
嗯,您可以根据需要添加任意多个索引。请注意,您的示例架构具有type
,但您的查询具有type_id
;当心由于键名错误而导致的静默不匹配。可能没有理由在price
上添加索引。
printjson ( db.foo.createIndex({point:"2dsphere"}) );
printjson ( db.foo.createIndex({type:1}) );
printjson ( db.foo.createIndex({category_id:1}) );