mongodb client

来源:互联网 发布:淘宝ip在哪看 编辑:程序博客网 时间:2024/06/05 08:47

from : http://www.mongodb.org/pages/viewpage.action?pageId=133415


C++ Tutorial

  • Installing the Driver Library and Headers
    • Unix
      • Full Database Source Driver Build
      • Driver Build
    • Windows
  • Compiling
  • Writing Client Code
    • Connecting
    • BSON
    • Inserting
    • Querying
    • Indexing
    • Sorting
    • Updating
    • Example
  • Further Reading

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

First, install Mongo -- see the Quickstart for details.

Next, you may wish to take a look at the Developer's Tour guide for a language independent look at how to use MongoDB.  Also, we suggest some basic familiarity with the mongo shell -- the shell is one's 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

A good source for general information about setting up a MongoDB development environment on various operating systems is the buildingpage.

The normal database distribution used to include the C++ driver, but there were many problems with library version mismatches so now you have to build from source. You can either get the full source code for the database and just build the C++ driver or download the driverseparately and build it.

Unix

For Unix, the Mongo driver library is libmongoclient.a. For either build, run scons --help to see all options.

Full Database Source Driver Build

To install the libraries, run:

scons --full install

--full tells the install target to include the library and header files; by default library and header files are installed in /usr/local.

You can use --prefix to change the install path: scons --prefix /opt/mongo --full install.

In version 2.0, you can also specify --sharedclient to build a shared library instead of a statically linked library. This feature is not yet working properly in 2.2, see SERVER-6514.

Driver Build

If you download the driver source code separately, you can build it by running scons (no options).

Windows

For more information on Boost setup see the Building for Windows page.

Compiling

The C++ driver requires the pcre and boost libraries (with headers) to compile.  Be sure they are in your include and lib paths. You can usually install them from your OS's package manager if you don't already have them.

Writing Client Code

Note: for brevity, the examples below are simply inline code.  In a real application one will define classes for each database object typically.

Connecting

Let's make a tutorial.cpp file that connects to the database (see client/examples/tutorial.cpp for full text of the examples below):

#include <cstdlib>#include <iostream>#include "mongo/client/dbclient.h"void run() {  mongo::DBClientConnection c;  c.connect("localhost");}int main() {  try {    run();    std::cout << "connected ok" << std::endl;  } catch( const mongo::DBException &e ) {    std::cout << "caught " << e.what() << std::endl;  }  return EXIT_SUCCESS;}

If you are using gcc on Linux or OS X, 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_filesystem -lboost_program_options -lboost_system -o tutorial$ ./tutorialconnected ok$
  • 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.

BSON

The Mongo 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 asBSONElement objects.  We use BSONObjBuilder to make BSON objects, and BSONObjIterator to enumerate BSON objects.

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 objects with a stream-oriented syntax:

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

The macro BSON 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 here.

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.

Querying

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;  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", 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 QUERY().  See classQuery 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.ensureIndex("tutorial.persons", fromjson("{age:1}"));

The ensureIndex method checks if the index exists; if it does not, it is created.  ensureIndex is intelligent and does not repeat transmissions to the server; thus it is safe to call it many times in your code, for example, adjacent to every insert operation.

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.ensureIndex("tutorial.persons", BSON( "age" << 1 ));

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", QUERY("age" << age));

to

to  auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", 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)));

Example

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 Mongo from C++.  There are many more capabilities.  For further exploration:

  • See the language-independent Developer's Tour;
  • Experiment with the mongo shell;
  • Review the doxygen API docs;
  • See connecting pooling information in the API docs;
  • See GridFS file storage information in the API docs;
  • See the HOWTO pages under the C++ Language Center
  • Consider getting involved to make the product (either C++ driver, tools, or the database itself) better!