Tutorial

Tutorial

Andrew C. Morrow edited this page on 18 May 2015 ·  40 revisions

Getting started with the Legacy C++ Driver

This is an introduction to usage of the MongoDB database from a C++ program.

NOTE: this tutorial is for the legacy and 26compat versions of the C++ driver.

First, install MongoDB – see the installation page for details.

Next, you may wish to take a look at the MongoDB Manual for a language independent look at how to use MongoDB. Also, we suggest some basic familiarity with the mongo shell – the shell is the primary database administration tool and is useful for manually inspecting the contents of a database after your C++ program runs.

Installing the Driver Library and Headers

Please see download and compile page for instructions on how to download, build, and install the C++ client driver.

Initializing the Driver Library

Please see the following wiki pages for instructions on how to properly initialize and terminate the driver:

Connecting

DBClientConnection

The C++ driver includes several classes for managing collections under the parent classDBClientInterface.

  • DBClientConnection is the connection class for connecting to a single MongoDB database server (or mongos)
  • DBClientReplicaSet is the connection class for connecting to a replica set.

See the API documentation for details on each of the above classes.

A simple program that connects to the database
#include <cstdlib>
#include <iostream>
#include "mongo/client/dbclient.h" // for the driver

void run() {
  mongo::DBClientConnection c;
  c.connect("localhost");
}

int main() {
    mongo::client::initialize();
    try {
        run();
        std::cout << "connected ok" << std::endl;
    } catch( const mongo::DBException &e ) {
        std::cout << "caught " << e.what() << std::endl;
    }
    return EXIT_SUCCESS;
}

(Note that in a production environment, the return value of mongo::client::initialize() must be checked)

If you are using gcc on Linux, you would compile with something like this, depending on location of your include files and libraries:

$ g++ tutorial.cpp -pthread -lmongoclient -lboost_thread-mt -lboost_system -lboost_regex -o tutorial
$ ./tutorial
connected ok

Warning

  • Since the tutorial program attempts to connect to a MongoDB database server, you must start it by running mongod before running the tutorial.
  • You may need to append -mt to boost_filesystem and boost_program_options. If using a recent boost, -mt is not needed anymore.
  • You may need to use -I and -L to specify the locations of your mongo and boost headers and libraries.
  • If using the 26compat branch you need to additionally specify -lboost_filesystem and -lboost_program_options

BSON

The MongoDB database stores data in BSON format. BSON is a binary object format that is JSON-like in terms of the data which can be stored (some extensions exist, for example, a Date datatype).

To save data in the database we must create objects of class BSONObj. The components of a BSONObj are represented as BSONElement objects. We use BSONObjBuilder to make BSON objects, and BSONObjIterator to enumerate BSON objects.

The C++ BSON Library

Include bson/bson.h in your application. See bsondemo for example usage.

Key classes
mongo::BSONObj: a BSON object
mongo::BSONElement: a single element in a BSON object. This is a key and a value.
mongo::BSONObjBuilder: used to make BSON objects
mongo::BSONObjIterator: used to enumerate BSON objects
Working with BSON

Let’s now create a BSON “person” object which contains name and age. We might invoke:

BSONObjBuilder b;
b.append("name", "Joe");
b.append("age", 33);
BSONObj p = b.obj();

Or more concisely:

BSONObj p = BSONObjBuilder().append("name", "Joe").append("age", 33).obj();

We can also create BSON objects using the stream oriented syntax:

BSONObjBuilder b;
b << "name" << "Joe" << "age" << 33;
BSONObj p = b.obj();

The BSON Macro lets us be even more compact:

BSONObj p = BSON( "name" << "Joe" << "age" << 33 );

Use the GENOID helper to add an object id to your object. The server will add an _id automatically if it is not included explicitly.

BSONObj p = BSON( GENOID << "name" << "Joe" << "age" << 33 );
// result is: { _id : ..., name : "Joe", age : 33 }

GENOID should be at the beginning of the generated object. We can do something similar with the non-stream builder syntax:

BSONObj p = BSONObjBuilder().genOID().append("name","Joe").append("age",33).obj();

Other helpers are listed BSON Helpers.

Inserting

We now save our person object in a persons collection in the database:

c.insert("tutorial.persons", p);

The first parameter to insert is the namespace. tutorial is the database and persons is the collection name.

getLastError

In order to ensure the write succeeded we need to call getLastError.

Get error result from the last operation on this connection:

string mongo::DBClientWithCommands::getLastError(); // Empty string if no error

Get the full last error object:

BSONObj DBClientWithCommands::getLastErrorDetailed();

For an example, see this demo.

For additional background information on getLastError see the write operations documentation.

Count

Let’s now fetch all objects from the persons collection, and display them. We’ll also show here how to use count().

cout << "count:" << c.count("tutorial.persons") << endl;

Query

auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", BSONObj());

while (cursor->more())
   cout << cursor->next().toString() << endl;

BSONObj() is an empty BSON object – it represents {} which indicates an empty query pattern (an empty query is a query for all objects).

We use BSONObj::toString() above to print out information about each object retrieved.BSONObj::toString is a diagnostic function which prints an abbreviated JSON string representation of the object. For full JSON output, use BSONObj::jsonString.

Let’s now write a function which prints out the name (only) of all persons in the collection whose age is a given value:

void printIfAge(DBClientConnection& c, int age) {
    auto_ptr<DBClientCursor> cursor =
        c.query("tutorial.persons", MONGO_QUERY("age" << age));
    while (cursor->more()) {
        BSONObj p = cursor->next();
        cout << p.getStringField("name") << endl;
    }
}

getStringField() is a helper that assumes the name field is of type string. To manipulate an element in a more generic fashion we can retrieve the particular BSONElement from the enclosing object:

BSONElement name = p["name"];
// or:
BSONElement name = p.getField("name");

See the api docs, and jsobj.h, for more information.

Our query above, written as JSON, is of the form

{ age : <agevalue> }

Queries are BSON objects of a particular format – in fact, we could have used the BSON() macro above instead of MONGO_QUERY(). See class Query in dbclient.h for more information on Query objects, and the Sorting section below.

In the mongo shell (which uses javascript), we could invoke:

use tutorial;
db.persons.find({age : 33});

Indexing

Let’s suppose we want to have an index on age so that our queries are fast. We would use:

c.createIndex("tutorial.persons", fromjson("{age:1}"));

In the above example we use a new function, fromjson. fromjson converts a JSON string to a BSONObj. This is sometimes a convenient way to specify BSON. Alternatively, we could have written:

c.createIndex("tutorial.persons", BSON( "age" << 1 ));

While calling createIndex multiple times won't result in duplicate index creation on the server, it will cause an extra network round-trip and server operation for each call. It's best practice to use this method sparingly, for example once at the beginning of your code, or perhaps in an external setup script that configures the database for your application. For more information about indexing, see theMongoDB Indexing docs.

Sorting

Let’s now make the results from printIfAge sorted alphabetically by name. To do this, we change the query statement from:

auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", MONGO_QUERY("age" << age));

to

auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", MONGO_QUERY("age" << age ).sort("name"));

Here we have used Query::sort() to add a modifier to our query expression for sorting.

Updating

Use the update() method to perform a database update. For example the following update in the mongo shell:

> use tutorial
> db.persons.update( 
    { name : 'Joe', age : 33 },
    { $inc : { visits : 1 } }
)

is equivalent to the following C++ code:

db.update("tutorial.persons",
    BSON("name" << "Joe" << "age" << 33),
    BSON("$inc" << BSON( "visits" << 1))
);

The update() method can be used to modify specific fields, replace the entire existing document, insert new documents or update multiple documents. In the MongoDB Manual, examples are provided in the Modify Documents Tutorial.

Arrays

A simple example illustrating usage of BSON arrays and the $nin operator is available here.

Further Reading

This overview just touches on the basics of using MongoDB from C++. There are many more capabilities. For further exploration:

TensorFlow是一个开源的机器学习框架,用于构建和训练各种机器学习模型。TensorFlow提供了丰富的编程接口和工具,使得开发者能够轻松地创建、训练和部署自己的模型。 TensorFlow Tutorial是TensorFlow官方提供的学习资源,旨在帮助新手快速入门。该教程详细介绍了TensorFlow的基本概念、常用操作和各种模型的构建方法。 在TensorFlow Tutorial中,首先会介绍TensorFlow的基本工作原理和数据流图的概念。通过理解数据流图的结构和运行过程,可以更好地理解TensorFlow的工作方式。 接下来,教程会详细介绍TensorFlow的核心组件,例如张量(Tensor)、变量(Variable)和操作(Operation)。这些组件是构建和处理模型的基本元素,通过使用它们可以创建复杂的神经网络和其他机器学习模型。 在教程的后半部分,会介绍如何使用TensorFlow构建不同类型的模型,例如深度神经网络(DNN)、卷积神经网络(CNN)和递归神经网络(RNN)。每个模型都会有详细的代码示例和实践任务,帮助学习者掌握相关知识和技能。 此外,教程还包含了关于模型的训练、评估和优化的内容,以及如何使用TensorBoard进行可视化和调试。 总结来说,TensorFlow Tutorial提供了全面而详细的学习资源,通过学习该教程,可以快速入门TensorFlow,并且掌握构建和训练机器学习模型的方法。无论是初学者还是有一定经验的开发者,都可以从中受益并扩展自己的机器学习技能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值