Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Update MongoDb sub document which is in a array  RSS feed

 
Kala Saseendran
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am pretty new to MongoDb. However i am trying to implement below functionality using java.

I Have a document as below. 

{
    "_id" : ObjectId("like-001"),
    "GroupID" : "1004",
    "likedBy" : [ 
        {
            "personId" : 123,
            "liked" : true
        },
 {
            "personId" : 167,
            "liked" : true
        },
 {
            "personId" : 467,
            "liked" : false
        }
    ],
    isdeleted:false
}



My functionality is, given a groupid, personId and Liked , find whether any document exist with given GroupId. If exist i should update that document with personId and Liked if the record not present, if person id also present I should not add the same person in array.

If the group id itself is not present I have to insert the entire document.

is there a way to do this with out making multiple calls to database. Also can this be done in bulk?



 
Vijitha Kumara
Saloon Keeper
Posts: 3997
42
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure what you mean by "...multiple DB calls to database." Of course you have to do a search/find and do an update as necessary. For bulk updates you may check on bulk write operations.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!