python:如何查找具有特定字段的文档

2024-06-12 21:48:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python和mongodb。我有一个藏品,里面有40000份文件。我有一组坐标,我需要找出这些坐标属于哪个文档。现在我在做:

cell_start = citymap.find({"cell_latlng":{"$geoIntersects":{"$geometry":{"type":"Point", "coordinates":orig_coord}}}})

这个方法是一个典型的geoJSON方法,它工作得很好。现在我知道有些文档有这样一个字段:

^{pr2}$

这个字段的值并不重要,所以我跳过它。问题是,我不需要从这40000个文档中查找文档,而是可以从字段名为'trips\udest'的文档中查找文档。在

因为我知道只有40%的文档有字段“trips_dest”,所以我认为这会提高效率。但是,我不知道如何修改我的代码。有什么想法吗?在


Tags: 方法文档mongodbtypecellfindstartpoint
1条回答
网友
1楼 · 发布于 2024-06-12 21:48:04

您需要$exists查询运算符。像这样:

cell_start = citymap.find({"trips_dest": {$exists: true},
                           "cell_latlng":{"$geoIntersects":{"$geometry":{"type":"Point", "coordinates":orig_coord}}}})

引用文档:

Syntax:{ field: { $exists: <boolean> } }

When <boolean> is true, $exists matches the documents that contain the field, including documents where the field value is null

如果需要拒绝null值,请使用:

^{pr2}$

最后一点需要说明的是,sparse index可能最终会加速这种查询。在

相关问题 更多 >