$geoNear, $near, and $nearSphere are not allowed in this context error with Mongo C driver

huangapple go评论59阅读模式
英文:

$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:

  1. Why does count() at the Mongo Shell work but mongoc_collection_count_documents() doesn't? Aren't they based on the same underlying operation?
  2. 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 but mongoc_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...)

huangapple
  • 本文由 发表于 2023年6月6日 03:54:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76409607.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定