Friday 15 June 2012

mongodb - Mongo C Driver accessing a document by its _id -


How can a document be used in the Mogo database with a Mongo-C-driver?

  void * get_doc_by_id (int obj_id) First of all, keep in mind that A  int  is not guaranteed to be sufficient to keep a MongoDB object ID (and in all cases I know)). The MongoDB object ID is 96-bit in size and  int  is typically 32-bit in size.  

You can try to do something like this:

  / ** * get_doc_by_id: * @conn: a Mongo connection * @ Db_and_collection: "db.collection" name place. * @Id: A BSNIID_T in which the document ID * * _id receives a document by field. * * Returns: Must be free from a new Beasan * * bson_destroy () * If successful; Otherwise on zero failure. * / BSN * get_doc_by_id (Mongo * cone, cons char * db_and_collection, cons BSNOID_T * oid) {BSN query; BSN * Rated = Faucet; Mongo_cursor * Cursor; Conn; (Db_and_collection) claims; Emphasis (OID); Bson_init (& amp; query); Bson_append_oid (& amp; Query, "_id", OID); Cursor = mongo_find (conn, db_and_collection, and query, tap, 1, 0, 0); If (Cursor!) {Goto cleanup; } If (mongoc_cursor_next (cursor)! = MONGO_OK) {Goto cleanup; } Rate = Colok (1, size * * right); If (! Ret) {Goto cleanup; } Bson_comopy (ret, mongoc_cursor_bson (cursor)); Cleanup: if (cursor) {mongo_cursor_destroy (cursor); } Bson_destroy (& amp; query); Return writ; }    

No comments:

Post a Comment