我正在搜索一系列商店。商店有一個嵌入式地點集合。我的目標是返回在地理位置附近有商店的商店集合,並且僅返回那些位置在之內的商店。在Mongoose附近使用elemMatch
我可以成功地限制查詢只返回商店使用「近」
Store
.where('isActive').equals(true)
.where('outlets.location')
.near({ center: [153.027117, -27.468515], maxDistance: 1000/6378137, spherical: true })
.where('outlets.isActive').equals(true)
.where('products.productType').equals('53433f1f3e02e39addde1954')
.where('products.isActive').equals(true)
.select('name outlets')
.select({'products': {$elemMatch: {'isActive': true, 'productType': '53433f1f3e02e39addde1954'}}})
.select('name outlets')
.execQ()
.then(function (results) {
console.log(results);
})
.fail(function (err) {
console.log(err);
})
.done();
具有在特定位置的出口我的問題是,這家店的文檔返回所有的網點,而不僅僅是出口與地理位置相匹配。我嘗試過使用elemMatch,像我對產品做過的那樣;
。選擇({ '網點':{$ elemMatch:{ '位置':{鄰近:{中心:[153.027117,-27.468515],maxDistance:六百三十七萬八千一百三十七分之一萬,球形:真}}}}} )
但是它返回一個空數組。在elemMatch子句中可以使用near操作符嗎?我做錯了嗎?是否有更高效/更快/更好的方式來實現目標?
答案中有些東西不清楚嗎?我認爲情況已經非常明確地解釋了。 –