Get data between two mongoDb dates using C Driver

i inserted such data

{"userid": "manaf", "DataValue": {"$ type": "00", "$ binary": "sampleBinaryData"}, "timestamp": 1460718961132, "_id": {"$ oid": " 5710cd7194e5f57831eea91e "}," __v ": 0}

I need to get b / w timestamp data.

i all this is done using the command below in the mongoDb client console.

db.sampleCollection.find ({"timestamp": {"$ gte": 1460703944149, "$ lt": 1460703944683}, "userid": "manaf"})

But I can not use this in my program c.

This is my client program.

#include <bson.h>
  #include <mongoc.h>
  #include <stdio.h>

  int
  main (int   argc,
        char *argv[])
  {
      mongoc_client_t *client;
      mongoc_collection_t *collection;
      mongoc_cursor_t *cursor;
      const bson_t *doc;
      bson_t *query;
      char *str;

      mongoc_init ();

      client = mongoc_client_new ("mongodb://localhost:27017/");
      collection = mongoc_client_get_collection (client, "sampledb", "sampleCollection");
      query = bson_new ();
      BSON_APPEND_UTF8 (query, "timestamp": {"$gte":1460703944149, "$lt":1460703944683 },"userid": "manaf");

      cursor = mongoc_collection_find (collection, MONGOC_QUERY_NONE, 0, 0, 0, query, NULL, NULL);
      printf("Started\n");
            while (mongoc_cursor_next (cursor, &doc)) {
          str = bson_as_json (doc, NULL);
          printf ("%s\n", str);
          bson_free (str);
      }

      bson_destroy (query);
      mongoc_cursor_destroy (cursor);
      mongoc_collection_destroy (collection);
      mongoc_client_destroy (client);
      mongoc_cleanup ();

      return 0;
  }

I got an error like this

error: macro "BSON_APPEND_UTF8" passed 4 arguments, but takes just 3
       BSON_APPEND_UTF8 (query, "timestamp": {"$gte":1460703944149, "$lt":1460703944683 },"userid": "manaf");

What is the actual problem with this program?

+4
3

, $ . BCON , .

#include <bson.h>
#include <bcon.h>

...

    query = BCON_NEW("$and","[",
                     "{", "timestamp", "{", "$gte", BCON_INT64(1460703944149), "}", "}",
                     "{", "timestamp", "{", "$lt", BCON_INT64(1460703944683), "}", "}","]");

...
0

, , , .

-, , , , BCON ( ), , , timestamp (: {"$gte":1460703944149, "$lt":1460703944683 }. JSON , , , . , , , , , BSON_APPEND_UTF8 , , UTF8. tutorial:

BSON_APPEND_UTF8() , { "hello": "world" }...

BSON_APPEND_UTF8 (query, "hello", "world");

timestamp , UTF8. , , $gte $lt, /. userId , , .

bson_append_<insert datatype here>, , - ( , , ):

bson_t *query;
bson_t timestampDoc;

query = bson_new();

BSON_APPEND_DOCUMENT_BEGIN(query, "timestamp", &timestampDoc);
bson_append_date_time(&timestampDoc, "$gte", -1, 1460703944149);
bson_append_date_time(&timestampDoc, "$lt", -1, 1460703944683);
bson_append_document_end(query, &timestampDoc);
BSON_APPEND_UTF8(query, "userId", "manaf");

, BCON:

bson_t *query;
query = BCON_NEW("timestamp", 
    "{", 
        "$gte", BCON_INT64(1460703944149),  
        "$lt", BCON_INT64(1460703944683), 
    "}",
    "userId", BCON_UTF8("manaf"),
);
0

BSON_APPEND_UTF8 :

BSON_APPEND_UTF8 (query, "\"timestamp\": {\"$gte\":1460703944149, \"$lt\":1460703944683 }","\"userid\": \"manaf\"");
-1

All Articles