1. Programming implementation
mongoc_client_t *client;
Mongoc_collection_t *collection;
mongoc_cursor_t *cursor;
Const bson_t *doc;
Bson_t *query;
Bson_t opts;
bson_t child;
Char *str;
bson_error_t err;
Mongoc_init ();
Bson_init (&opts);
Client = Mongoc_client_new ("mongodb://localhost:27017");
Collection = mongoc_client_get_collection (client, "MyDB", "Mycoll");
query = Bson_new ();
Bson_append_utf8 (query, "Hello", "world2");
Bson_append_document_begin (&opts, "projection", &child);
Bson_append_int32 (&child, "_id", 0);
Bson_append_document_end (&opts, &child);
cursor = mongoc_collection_find_with_opts (collection, query, &opts, NULL);
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;
{"Hello": "World2"}
2.shell command
To remove the _id field,
Db.mycoll.find ({hello: ' world '}, {_id:0})