Extracting specific values from documents - using C Driver

Hey there,
I am new to MongoDB. I have a few basic questions regarding the extraction of data. I am working on a little project where order data is stored on MongoDB and I need to extract certain parts of it with a C program running on ubuntu. So every time a order arrives, a new document with all the necessary data is uploaded to a collection. The data contains mostly of float or double variables.

I already installed the C drivers and libraries on my ubuntu pc, established a connection and I am able to list the collections or documents for example. But now I am a little bit lost, mainly because I do not know the best approach to solve my problem, because there seem to be so manny different ways to do it.

Do I need to download or locally store a document to be able to search it? Or to read it? Do I need to define an own bson document in order to fill it with the data extracted or can I just extract integers or characters? What is the best way to do it?

I would appreciate a hint in the right direction and the needed functions

thanks a lot :slight_smile:

Hello @Alexander_Laub, welcome to the MongoDB Community forum!

Here is some info and hope is useful to you.

Do I need to download or locally store a document to be able to search it? Or to read it?

When you perform a read operation on a collection, you get some/one/none documents from the database into your application. The number of documents returned depend upon the filter you specify in the find method. Then there is a feature called as projection. Projection allows you specify in the find method what specific fields of the document you want to retreive from the database for the matching filter. The search happens on the database server and the application receives the result.

The find method retreives a cursor into the application, which can be used to read the documents. The cursor will be empty, if there is no match for the specified filter.

Consider multiple documents in a collection in the following format:

{ _id: 1, name: "John", city: "New York", country: "US" }
{ _id: 2, name: "Jane", city: "New York", country: "US" }
{ _id: 3, name: "Raj", city: "Delhi", country: "India" }

The query:

db.collection.find( { city: "New York" }, { name: 1, city: 1, _id: 0 } )

In the above query:

{ city: "New York" } is the query filter.
{ name: 1, city: 1, _id: 0 } is the projection.

The query returns a cursor with two matching documents. The output document structure will be like this with two fields:
{ name: "John", city: "New York" }


Do I need to define an own bson document in order to fill it with the data extracted or can I just extract integers or characters?

The returned documents are to be loaded into some kind of structure / records (I am not familiar with C language terminology) within your application, and use it further.


Reference:


EDIT ADD:

MongoDB data is stored as BSON types. In general, when you store a number in the database it is by default a double (other numeric data types are int, long and float, and there is a NumberDecimal type mainly used for precise decimal numbers like currency). You can check the data type of a field using the $type operator on a specific field. There is mapping from the BSON to the programming language types as part of the driver functionality. Each programming language has their own mappings.

That was very helpfull indeed. Thanks.

As you suggested, I trief a find() function. In my case I used mongoc_collection_find_with_opts() I tried following the example further down called print_ten_documents(). I managed to access some of my documents and print them to the command prompt as a whole, I see that as a win.

But I have some question concerning the program flow (not about the syntax :slight_smile: ):

In my main code I gain access to my database via a client. The example function print_ten_documents() needs a pointer to a collection. I get the collection the mongoc_client_get_collection() function. But I assume, the collection as a whole will be downloaded and stored locally in my memory?

Then I pass the collection to the print_ten_documents() function which uses the mongoc_collection_find_with_opts() function to place the cursor you mentioned at the first document and then loops a print command over 10 documents.

I assume I can not use your db.collection.find() and need to use the functions provided by the C driver? I could not get it to work, is this just for the use in the MongoDB shell maybe?

I am unable to find a clear specification for the queries filter and opt. I would like to know about their structure, for example what to modify so only certain fields are printed. And just disable the filter for the moment, somehow I can’t get it to work.

Another question: If my assumption is correct and for reading/finding the complete collection is beeing downloaded so it can be referenced via a pointer, what happens when I want to write something to a document? Is the document downloaded, altered and then uploaded, replacing the original one or how does this work?

Nevermind about the opt specifications I found them. But I dont know how to edit my post. :smiley:

Hello @Alexander_Laub, here are some clarifications.

I get the collection the mongoc_client_get_collection() function. But I assume, the collection as a whole will be downloaded and stored locally in my memory?

Its just a reference or a pointer.


Then I pass the collection to the print_ten_documents() function which uses the mongoc_collection_find_with_opts() function to place the cursor you mentioned at the first document and then loops a print command over 10 documents.

The mongoc_collection_find_with_opts() and the cursor aspect - are correct.


I assume I can not use your db.collection.find() and need to use the functions provided by the C driver? I could not get it to work, is this just for the use in the MongoDB shell maybe?

I suspect there are corresponding functionality in C driver (the drivers I had tried have all the fucntionality from shell - NodeJS, Java, C#, Python and Golang). Note the mongo shell has its own driver too. Also, browse the MongoDB C driver APIs.


…what happens when I want to write something to a document? Is the document downloaded, altered and then uploaded, replacing the original one or how does this work?

You use an update operation to change the document data. To change the city field in the collection, you do this on the client:

db.collection.update( { name: "John" }, { $set: { city: "Chicago" } } )

When you run the update query, it is sent to the database server, where the query filter { name: "John" } is applied and the document is changed as per the update { $set: { city: "Chicago" } }. It is an atomic operation at the document level. You get an update result confirming the update operation is complete/success (or an error if failure) from the server.

Ok. Interesting.

I found all the different query options and would now like to extract some data form my documents, just like the title says :smiley: .

How do I do this? What would be the best way? I assume I still need the cursor for this. If I call my find() function with all the filters and opts queries active, the function gives back a cursor. What exactly does this cursor mark? Every field entry of every document that suits the search, simultaneously? How can I for example store the integer data of the field called “Age” to a C integer? How do I navigate the cursor to the next documents field entry “Age”?

Here is the link with couple of examples about using the opts of the find method. The opts are used for actions like sorting, limit and projection (please see my earlier post and example about what is projection) on the query returned documents.

http://mongoc.org/libmongoc/current/mongoc_collection_find_with_opts.html#examples