Simple Example of MongoDB C ++ Application

Source: Internet
Author: User
Tags mongodb server

Assuming that you already have MongoDB server and work on Ubuntu and install GCC, the following C ++ program can help you quickly enter the state where you can use MongoDB.

#include <cstdlib>#include <mongo/client/connpool.h>using namespace std;using namespace mongo;void SaveStatus(mongo::DBClientBase& session, string const& display_id, string const& status) {        BSONObjBuilder condition;    condition.append("display_id", mongo::OID(display_id));    BSONObjBuilder status_field;    status_field.append("status", status);    BSONObjBuilder data;    data.append("$set", status_field.obj());    session.update("mydb.mycoll", mongo::Query(condition.obj()), data.obj());}/* *  */int main(int argc, char** argv) {    mongo::ScopedDbConnection con("localhost", 5000);//timeout is 5000    mongo::DBClientBase& session = con.conn();            BSONArrayBuilder display_ids;    display_ids.append(mongo::OID("5061f915e4b045bab5e0c957"));    BSONObjBuilder in_condition;    in_condition.append("$in", display_ids.arr());        BSONObjBuilder message_condition;    message_condition.append("display_id", in_condition.obj());    message_condition.append("status", "sending");        BSONObjBuilder sortBuilder;    sortBuilder.append("_id", 1);    mongo::Query query(message_condition.obj());    mongo::Query query2(query);    mongo::Query query_with_sort(query.sort(sortBuilder.obj()));    BSONObjBuilder update_field;    update_field.append("status", "waiting");    BSONObjBuilder set_field;    set_field.append("$set", update_field.obj());    session.update("mydb.mycoll", query2, set_field.obj(), false, true);            con.done();    return 0;}

Tip:

1. The savestatus function shows how to set a field value.

2. The main function demonstrates more complex usage and uses $ in as the query condition of the update statement.

3. The final con. Done () function call is required because the connection must return to the pool.

4. session. the update query condition cannot contain sort. Otherwise, the update will not succeed. Therefore, we can see that my code copies a query2 object for update, while query_with_sort can be used for general queries (FIND ).

5. The OBJ method of bsonobject can only be called once, and the second program will crash.

6. Mongo: oid is used to convert a string to an oId object, which is a common _ id value.

7. Sometimes, when a compilation error occurs, you can try this header file:

#include "mongo/client/dbclient.h"

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.