0

I have a json object as following:

{ "_id" : ObjectId("508806803bb97dc546e6f307"), "user_name" : "user1", "user_id" : 45645645, "likes" : [ { "event_id" : NumberLong("4578541212") },{ "event_id" : NumberLong("4578541213") } ], "dislikes" : [ ] }

I'm trying to delete specific event within likes array via java drivers tried doing this first in shell:

> db.users.update( {'likes.event_id' : 4578541212}, { '$unset':{'likes.event_id'
:1}})

with no luck...how can I manage doing that?

1
  • Do you want to remove the event_id field from the array element (leaving an empty element) or remove the element itself? Commented Oct 24, 2012 at 20:24

1 Answer 1

1

If you want to just remove the event_id field from the array element:

db.users.update( {'likes.event_id' : 4578541212}, {'$unset':{'likes.$.event_id' :1}})

Use the $pull operator to delete the element:

db.users.update({'likes.event_id': 4578541212}, {'$pull':{likes: {event_id: 4578541212}}})
Sign up to request clarification or add additional context in comments.

1 Comment

thanks alot it worked, but I have another question: as I executed the command you suggested the whole record was messed up (fields switch places), any idea why is this happning?

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.