Above query simply means that timestamp > 142400000 and timestamp < 142500000, speed = 0 and description having binary data equivalent to “Binary”. BSONBinData is the Object that is required to be passed to the MONGO_QUERY macro for performing the comparison.

Executing Query

Now we have made the connection and the query, next we will execute this query on the server. In this we shall be using the database “mydb” and collection name “Locations”

We now declare a cursor variable which will return pointer to the records formatted in the BSONObj format. cursor->more() checks if we have any more rows to be retrieved from the server and if yes we call cursor->next() which returns BSONObj object.

In order to retrieve N number of records we can pass additional parameter to the query function with number of records required to be fetched from the server. This is equivalent to the LIMIT clause in WHERE statement of SQL

Data Retrieval from BSONObj

Finally we are required to retrieve various fields from the BSONObj given by the cursor->next()

In the above case we always assume that the data is inserted in the Double format inside the collection. Sometimes it becomes necessary to evaluate the type and perform conversion as per your requirement. We can achieve it with the following code

double latitude;

while (cursor->more()) {

p = cursor->next();

// Do something with the fetched record

switch (p.getField(“latitude”)) {

case mongo::NumberDouble:

latitude = p.getField(“latitude”).Double();

break;

case mongo::String:

{

string lat = p.getField(“latitude”).String();

sscanf (lat.c_str(), “%lf”, &latitude);

}

break;

}

}

Count

Counting number of records present in the collection

cout << “count:” << conn.count(“mydb.Locations”) << endl;

Insert Rows into collection

Inserting rows into MongoDB required usage of BSONObjBuilder. Following code will illustrate on addition of 4 fields in the object and then inserting it into the collection

//Create BSONObjBuilder type Object

obj = new mongo::BSONObjBuilder();

//Append various fields and set their values

obj->append(“latitude”, 27.24424);

obj->append(“longitude”, 78.24242);

obj->append(“speed”, 0);

obj-appendBinData(“description”, 6, mongo::BinDataGeneral, “Binary”);

//Perform insertion command

conn.insert(dbCollection, obj->obj());

std::string e = conn.getLastError();

if (!e.empty()) {

MongoException m(1, “insert failed: ” + e);

return -1;

}

Indexing

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

conn.createIndex(“mydb.Locations”, fromjson(“{timestamp: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:

conn.createIndex(“mydb.Locations”, BSON( “timestamp” << 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.

Updating

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