0

I have stuck in a situation which working on MongoDB.

Below is the structure of my mongoDB :

<pre>
{                                                                                 
        "_id" : ObjectId("53565ae90edaafbe4a7f890b"),                             
        "email" : "[email protected]",                                       
        "fname" : "abc",                                                        
        "lname" : "cde",                                                     
        "Password" : "sadasdsadasdasdasdaasasdas",                                
        "Start Date" : "Start Date",                                              
        "Inactive Date" : "Inactive date",                                        
        "DOB" : "Date of Birth",                                                  
        "Address" : {                                                             
                "Line1" : "Line1",                                                
                "Line2" : "Line2",                                                
                "city" : "City",                                                  
                "State" : "Delhi"                                                 
        },                                                                        
        "newsfeed" : [                                                            
                {                                                                 
                        "post_id" : ObjectId("535671910edaafbe4a7f8918"),         
                        "post_by" : ObjectId("53565bb00edaafbe4a7f890d"),         
                        "Post_content" : "content of the post",                   
                        "likes" : [ ],                                            
                        "comments" : [ ]                                          
                },                                                                
                {                                                                 
                        "post_id" : ObjectId("535671920edaafbe4a7f8919"),         
                        "post_by" : ObjectId("53565bb00edaafbe4a7f890d"),         
                        "Post_content" : "content of the post",                   
                        "likes" : [ ],                                            
                        "comments" : [ ]                                          
                }                                                                 
        ]                                                                         
}        
</pre>

While practicing, i was trying to emulate the data model for a social networking site such as facebook. Now i wanted to update the likes array where post ID is ObjectId("535671920edaafbe4a7f8919"). How can i do this without using any scripting language and straight from mongoshell by update statement.

Query parameter in update statement does not seems to work and it does not locate the exact position of ObjectId("535671920edaafbe4a7f8919").

any help will be appreciated.

2 Answers 2

1

Are you looking for something like this, or do you want finer control over the likes array itself?

db.coll.update(
  {"newsfeed.post_id" : ObjectId("535671920edaafbe4a7f8919")},
  { $set : { "newsfeed.$.likes" : ["value1", "value2", "value3"] }
);

http://docs.mongodb.org/manual/reference/method/db.collection.update/#update-an-element-if-position-is-unknown

Sign up to request clarification or add additional context in comments.

1 Comment

Yes. Thanks. Actually i misunderstood $ operator. I though it picked the first result and updates it. thanks a lot you are bang on. But just to be sure is there way to get index of that sub document in that array. Will make things much easier. Thanks.
0

You could use the positional $ operator in a projection:

db.coll.find({"newsfeed.post_id" : ObjectId("535671920edaafbe4a7f8919")}, 
             {"newsfeed.$" : 1});

resulting in

{ "_id" : ObjectId("53565ae90edaafbe4a7f890b"), 
  newsfeed : [{                                                                 
                    "post_id" : ObjectId("535671920edaafbe4a7f8919"),         
                    "post_by" : ObjectId("53565bb00edaafbe4a7f890d"),         
                    "Post_content" : "content of the post",                   
                    "likes" : [ ],                                            
                    "comments" : [ ]                                          
            }] }

However, this design is hard to work with, it doesn't scale, and it is very inefficient because of the growing documents. Since there are a lot of things to consider when designing a news feed with MongoDB, I'd like to recommend a blog article I wrote two years ago, which highlights some of the issues. There are certainly more resources about this topic out there.

1 Comment

Yes. Thanks. Actually i misunderstood $ operator. I though it picked the first result and updates it. Both of you go the answer correct. I was able to accomplish the job. But just to be sure is there way to get index of that sub document in that array. Will make things much easier. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.