MongoDB How to select a query result specify field remove _id field

Source: Internet
Author: User
Tags mongodb

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})



  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.