Question

I have stuck in a situation which working on MongoDB.

Below is the structure of my mongoDB :

<pre>
{                                                                                 
        "_id" : ObjectId("53565ae90edaafbe4a7f890b"),                             
        "email" : "abc@cde.com",                                       
        "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.

Was it helpful?

Solution

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

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top