Two small lessons about MongoDB API

Source: Internet
Author: User

First, preface

MongoDB is also an alternative platform for projects that need to be manipulated in Java and C + +. Not much else to say, several key requirements:

1 collection columns have int, long and bindata (also known as binary array) types;

2 Java and C programs write data that can be read from each other.

Second, Java implementation Bindata read

Java Engineering to introduce Mongo-java-driver.jar dependency can be, sample code a lot, here is not posted.

The other column is simple, just a bit of a hassle when reading Bindata, because MongoDB's Java API does not directly provide interfaces such as Getbinary.

The solution is: introduce the Org.bson.types.Binary class, and then convert it:

     Binary binary = (binary) doc.get ("Data");     Obj.setdata ((byte[]) binary.getdata ());

By the way, write Bindata directly with Doc.append (), no need to worry.

In addition, the C + + API uses Mongo-c-driver, which has a dedicated interface to Bindata and is relatively straightforward.

Third, the reading order of C + + program

There was a strange problem in the testing process, that is, the data written by C + + programs, the Java program can be fetching read, and vice versa.

Toss half a day after finding the reason: the Java API reads with the DOC.GETXXXX (String) method, regardless of the order.

mongocursor<document> cursor = collection.find (docfilter). iterator ();          while (Cursor.hasnext ())        {            = cursor.next ();             // reading order does not matter            int col2 = Doc.getinteger ("COL2"). Intvalue ();             long col3 = Doc.getlong ("COL3"). Longvalue ();             int col1 = Doc.getinteger ("COL1"). Intvalue ();            ...        }

In the C + + API, reading is a combination of bson_iter_find (bson_iter_t*, const char*) and BSON_ITER_XXXX (bson_iter_t*) functions, where order is important, Must be consistent with writes (the write order and storage order are also consistent).

mongoc_cursor_t cursor = mongoc_collection_find_with_opts (collection, query, NULL, NULL);

while (Mongoc_cursor_next (cursor, &doc))
{

int col1, col2;

Long col3;

bson_iter_t ITER;

Bson_iter_init (&iter, doc);

Must be consistent with write-time order

if (Bson_iter_find (&iter, "COL1"))

col1 = Bson_iter_int32 (&iter);

if (Bson_iter_find (&iter, "COL3"))

Col3 = Bson_iter_int64 (&iter);

if (Bson_iter_find (&iter, "COL2"))

col2 = Bson_iter_int32 (&iter);

...

}

Assuming that the order in which the data is written is COL1, COL2, COL3, only the values of col1 and col2 can be obtained in the previous code, and Col3 will be skipped.

Project, the write order of Java programs and C/C + + programs is aligned, and then read in the same order in C + + programs, solving the problem.

Two small lessons about MongoDB API

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.