我在ANSI C代码中使用mongoc库。我是mongoc API的新手,我在创建查询时遇到了问题。下面的代码抛出断言src / bson / bcon.c:807:bcon_append_ctx_va:断言`ctx-> n!= 0'失败。有人可以帮忙吗?
bool is_point_near_road(LOCATION_ITEM* item)
{
bson_error_t error;
const bson_t *doc;
char *str;
query = BCON_NEW ("road_segment:", "{",
"$near:", "{",
"$geometry:", "{",
"type:", "Point", "coordinates:", "[",BCON_DOUBLE(27.9478454), ",", BCON_DOUBLE(65.6503487), "]",
"}", "$maxDistance:", BCON_INT32(500),
"}",
"}",
);
cursor = mongoc_collection_find(collection_query, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
if (cursor == NULL)
{
printf ("Cursor is NULL");
}
while (mongoc_cursor_more (cursor) && mongoc_cursor_next (cursor, &doc)) {
str = bson_as_json (doc, NULL);
printf ("STR: %s\n", str);
bson_free (str);
}
if (mongoc_cursor_error (cursor, &error)) {
fprintf (stderr, "An error occurred: %s\n", error.message);
}
mongoc_cursor_destroy (cursor);
bson_destroy (query);
return true;
}
答案 0 :(得分:0)
我不明白你的错误,但我注意到你的查询至少有一个问题,就是“:”。
我猜你是从一个使用mongo客户端的例子开始的(我们在附近写了$ :)。对于BCON_NEW,不需要“:”。
我刚写了一个类似于你的查询,但它确实有用。尝试从查询中删除:
另外,不要在坐标上添加“,”,只需将它们添加到数组中即可。
最后,我使用mongoc_collection_find_with_opts
===========
query = BCON_NEW ("road_segment", "{",
"$near", "{",
"$geometry", "{",
"type", "Point",
"coordinates", "[",
BCON_DOUBLE(27.9478454),
BCON_DOUBLE(65.6503487),
"]",
"}",
"$maxDistance", BCON_INT32(500),
"}",
"}");