0

I'm using mongoc library in ANSI C code. I'm new with mongoc API and I have problem in creating query. Following code throws assertion src/bson/bcon.c:807: bcon_append_ctx_va: Assertion `ctx->n != 0' failed. Can someone help?

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;
} 
ah laht
  • 1
  • 2

1 Answers1

0

I don't understand the error you got, but I notice that your query has at least one problem, it's the ":".

I guess you started from an example using mongo client (where we write $near:). For the BCON_NEW, there is no need for the ":".

I just wrote a query similar to yours and it works. Try to remove the : from your query.

Also, don't add "," to your coordinates, you just add them to the array.

Last thing, I use 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),
    "}",
"}");
Oussama
  • 1
  • 1