[英]Pymongo query with $lte and $gte not working
我在mongo中有以下文檔:
{
'Name': 'Dummy',
'North-East-Bound': {
'lat': 0,
'lng': 0
},
'South-West-Bound': {
'lat': 0,
'lng': 0
}
}
我正在執行以下查詢:
result = self.coll.find_one({
'North-East-Bound':
{'lat': {'$gte': lat},
'lng': {'$gte': lng}
},
'South-West-Bound':
{'lat': {'$lte': lat},
'lng': {'$lte': lng}
}
})
顯然,我使用lat = 0和lng = 0作為參數。 我希望返回虛擬文檔,但我什么也沒有。 我做錯了什么?
謝謝。
您必須使用點符號在嵌入式文檔的字段上運行查詢。
find_one({
'North-East-Bound.lat': {
'$gte': lat
},
'North-East-Bound.lng': {
'$gte': lng
},
'South-West-Bound.lat': {
'$lte': lat
},
'South-West-Bound.lng': {
'$lte': lng
}
})
這將用於平等比較。 這是嵌入式文檔級別的比較。
find_one({
'North-East-Bound': {
'lat': 0,
'lng': 0
},
'South-West-Bound': {
'lat': 0,
'lng': 0
}
})
這里更多
https://docs.mongodb.com/manual/core/document/#dot-notation
https://docs.mongodb.com/manual/tutorial/query-embedded-documents/#query-on-nested-field
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.