Hey there,
I have a document that consists of multiple nested objects?, I don’t know if thats correct. In the example below there are two of these objects “Skill_1” and “Skill_2”.
"Skill_1": {
"Name_Skill": "DrillHole",
"Status_Skill": "PENDING",
"Parameter": "10",
},
"Skill_2": {
"Name_Skill": "DrillSlot",
"Status_Skill": "PENDING",
"Parameter": "11",
}
My problem is that I would like to access the value of Skill_1’s “Parameter”.
cursor = mongoc_collection_find_with_opts(collection, filter, opts, NULL);
while(mongoc_cursor_next(cursor, &doc)) {
if(bson_iter_init(&iter, doc)) {
bson_iter_find(&iter, "Skill_1.Parameter");
variable = bson_iter_as_double(&iter);
printf("%f\n, variable);
}
}
This approach worked as long as I was working without nested fields. I would like to store the value as a double and the name/key of the field in a char array. Is it possible to store just the last part of the name/key, so just “Parameter”?
Thx
Hello @Alexander_Laub,
I want to suggest a better way of structuring the data you had posted. Ideally, the data should be as follows. There is an array field skills
with sub-documents (nested) as array elements. This will allow more than two skills (or as more skills might show up later):
skills: [
{
id: 1,
name: "DrillHole",
status: "PENDING",
parameter: "10"
},
{
id: 2,
name: "DrillSlot",
status: "PENDING",
parameter: "11"
}
]
To access the skill 1’s sub-document, the mongo
shell query would be:
db.collection.find({ "skills.id": 1 }, { "skills.$": 1, _id: 0 } )
The output has the skill 1’s data:
{
"skills" : [
{
"id" : 1,
"name" : "DrillHole",
"status" : "PENDING",
"parameter" : "10"
}
]
}
Okay thank you very much for your suggestion. Does a sub-document need an id just as the real document?
I think we already had the pleasure so I know you are working with the shell and that you are not familiar with the C-Driver syntax, but my main problem remains. If I try to iterate over the field of the main document to access the values, I can only iterate over the sub-documents, or if I follow your suggestion, over the arrays of sub-documents.
I don’t know how I can get the iteration to ENTER the sub-document. But after initializing the cursor, I can however print every field of the document and it’s sub-documents to the console, so this must be possible or am I mistaken?
There are two functions, bson_iter_document() and bson_iter_array() which should both deliver raw buffer data of the document/array passed to them, maybe I can try this and extract the data byte-wise?
Not required, unless you think its useful within your application.
If I try to iterate over the field of the main document to access the values, I can only iterate over the sub-documents, or if I follow your suggestion, over the arrays of sub-documents.
Consider a single document in the collection.
Consider the structure of the original one, you do not iterate over the nested documents Skill_1
and Skill_2
. In the mongo
shell you access a nested document field using a dot-notation; for example: “Skill_1.Parameter”. This notation can be used within the query filter as well as the projection. Reference: Query on Embedded/Nested Documents.
Consider the structure I had suggested. The query I had posted in my earlier post is the way to get to a specific sub-document.
MongoDB supports various operators to work with array fields. Add nested document to an array field. Also, modify a specific nested document or delete one. Further there are operators to query, filter, perform projection, iterate, etc. This is de-normalized data model at work.