top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to update nested array in mongodb

+1 vote
265 views

I have the following collections,

{ 
    "_id" : ObjectId("55884eddd00431413bdd1d97"),
    "username" : "test",
    "reports":[
       "folder 1":[{
         "file1":{
           "ceratedBy":"user1"
         },

         "file2":{
           "ceratedBy":"user2"
         } 
       }],

       "folder 2":[{
         "file1":{
           "ceratedBy":"user1"
         },

         "file2":{
           "ceratedBy":"user2"
         } 

       }]
     ]}

I now want to insert into the reports.folder 1 array say "file 3":{"cerated by":"user 3"}. Appreciate any help

posted Jun 25, 2015 by anonymous

Looking for an answer?  Promote on:
Facebook Share Button Twitter Share Button LinkedIn Share Button

Similar Questions
0 votes

My collection is like this, but I want to update a nested object with {"env":"qa"} in my updated collection with java-driver:

{ "_id" : ObjectId("5b052eeff9290437b217b1ed"), "app" : "hike", "group" : [ { "env" : "prod" }, { "env" : "test" } ]}{ "_id" : ObjectId("5b052f36f9290437b217b1ee"), "app" : "viber", "group" : [ { "env" : "prod" }, { "env" : "test" } ]}

0 votes

In our solution we have asynchronous processes with state persistence (aka sagas) where each saga has single state document persisted in mongodb. Depending on the saga type, this document may lead to high level of concurrent updates. So, we simply introduced optimistic concurrency there to retry handling of incoming messages. But as a result of that we face very high rate of retries what results in significant performance degradation. In the past we used pessimistic locking of saga document/record when used to store state in rdbms (mssql, oracle etc). I know that at the moment there is no such feature in mongodb, but maybe it would be useful/possible to have it for single document updates? I believe this feature could simplify many solutions.

Are there any other concurrency handling options? Suggestions?

+3 votes

I added high number of query to a db, somehow it impact query to another db. I am using mongodb 2.6.8. Should this version of mongodb has db level lock (since 2.2)?

Is this I/O disk problem?

+2 votes

I get duplicates of fields and subfield using aggregation. Searching for duplicated sub field "k01.v" works fine:

db.table_0.aggregate({"$group" : { _id: "$k01.v", "count": { "$sum": 1 } } }
{"$match": {"count" : {"$gt": 1} } }
{"$project": {"k01.v" : "$_id", "_id" : 0} });

Unfortunately, this doesnt work when searching for sub field "a1.0" (field "a1" is an array):

db.table_0.aggregate({"$group" : { _id: "$a1.0", "count": { "$sum": 1 } } }
{"$match": {"count" : {"$gt": 1} } }
{"$project": {"a1.0" : "$_id", "_id" : 0} });

Any workaround ?

...