0

我在 ANSI C 代码中使用 mongoc 库。我是 mongoc API 的新手,在创建查询时遇到问题。以下代码抛出断言 src/bson/bcon.c:807: bcon_append_ctx_va: Assertion `ctx->n != 0' failed。有人可以帮忙吗?

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;
} 
4

1 回答 1

0

我不明白您遇到的错误,但我注意到您的查询至少有一个问题,它是“ ”。

我猜你是从一个使用 mongo 客户端的示例开始的(我们在其中编写 $near:)。对于 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),
    "}",
"}");
于 2018-04-25T14:50:35.963 回答