英文:
$geoNear, $near, and $nearSphere are not allowed in this context error with Mongo C driver
问题
I'm using the mongoc_collection_count_documents() function (driver version 1.23.1, MongoDB server version 5.0.13), this way:
long long n = mongoc_collection_count_documents(collection, q, NULL, NULL, NULL, &error);
where q
is a bson_t
variable that depends on the particular query.
With simple queries like { "score": 20 }
it works fine. However, if I try something like this:
{
"location": {
"$near": {
"$geometry": {
"type": "Point",
"coordinates": [
2,
1
]
},
"$maxDistance": 10
}
}
}
(I have a 2dsphere
index for the location
field)
I get the following error:
$geoNear
, $near
, and $nearSphere
are not allowed in this context.
On the contrary, if I run the same query with db.c.count()
at the Mongo Shell I don't get any error.
So I have two questions:
- Why does
count()
at the Mongo Shell work butmongoc_collection_count_documents()
doesn't? Aren't they based on the same underlying operation? - How can I get a count of documents for queries using
$near
, etc. with the Mongo C driver?
英文:
I'm using the mongoc_collection_count_documents() function (driver version 1.23.1, MongoDB server version 5.0.13), this way:
long long n = mongoc_collection_count_documents(collection, q, NULL, NULL, NULL, &error);
where q
is a bson_t
variable that depends on the particular query.
With simple queries like {"score": 20}
it works fine. However, if I try something something like this:
{
"location" : {
"$near" : {
"$geometry" : {
"type" : "Point",
"coordinates" : [
2,
1
]
},
"$maxDistance" : 10
}
}
}
(I have a 2dsphere
index for location
field)
I get the following error:
$geoNear, $near, and $nearSphere are not allowed in this context
On the contrary, if I run the same query with db.c.count()
at the Mongo Shell I don't get any error.
So I have two questions:
- Why
count()
at the Mongo Shell works butmongoc_collection_count_documents()
doesn't? Aren't they based in the same underlying operation? - How I can get a count of document for queries using
$near
etc. with Mongo C driver?
答案1
得分: 0
由于由@Joe提供的链接,我找到了使用$geoWithin
和$center
而不是$near
的建议。
问题是$geoWithin
没有$near
的$minDistance
等价物。然而,根据MongoDB JIRA上的此票,可以这样构造查询(例如,最大距离为10,最小距离为5):
{
$and: [
{ location: { $geoWithin: { $centerSphere: [ [ 2, 1 ], 10 ] } } },
{ location: { $not: { $geoWithin: { $centerSphere: [ [ 2, 1 ], 5 ] } } } }
]
}
更复杂,但足够满足我的需求(尽管我不知道为什么“紧凑的方式”现在被视为在计数函数中不推荐使用...)。
英文:
Thanks to the link provided by @Joe I have find the suggestion to use $geoWithin
with $center
instead of $near
.
The problem is that $geoWithin
doesn't have a equivalence for $near
's $minDistance
. However, looking to this ticket at MongoDB JIRA the query can be formulated this way (e.g. being 10 max distance and 5 min distance):
{
$and: [
{ location: { $geoWithin: { $centerSphere: [ [ 2, 1 ], 10 ] } } },
{ location: { $not: { $geoWithin: { $centerSphere: [ [ 2, 1 ], 5 ] } } } }
]
}
More complicated, but it suffices my case (although I wonder why the "compact way" is now considered deprecated in count functions...)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论