Stickup Artist
Stickup Artist

Reputation: 119

How to remove specific documents from MongoDB?

Trying to remove specific chats from a Mongo collection.

Sample Chat in MongoDB:

{ "_id" : "4jkq6fJbNtwEGxTzv", "user1Id" : "czA8espdEbTgz7HqG", "user2Id" : "WyvyFaAdGqEKJgcni", "messages" : [ { "timeStamp" : "Wed, 09 Nov 2016 06:19:08 GMT", "sentBy" : "czA8espdEbTgz7HqG", "text" : "testing123" } ] }

Tried:

db.chat_users.remove( {"_id": ObjectId("4jkq6fJbNtwEGxTzv")});

But got:

Error: invalid object id: length

Tried:

db.user_track.remove( { access_time : {"$lt" : new Date(2016, 12, 1) } })

But got:

WriteResult({ "nRemoved" : 0 })

Option 1: Why are my object ID's too short?

Option 2: Why weren't chats created before December 1 removed?

Any ideas how I can remove a bunch of chats from Wed, 09 Nov 2016?

Thank you.

Upvotes: 0

Views: 60

Answers (1)

Mohsen ZareZardeyni
Mohsen ZareZardeyni

Reputation: 946

Your id class is not ObjectId! mongo stores a normal ObjectId like this:

"_id" : ObjectId("583aca7d7882395095bef1fc")

So change your code to this:

db.chat_users.remove( {"_id": "4jkq6fJbNtwEGxTzv"});

Upvotes: 3

Related Questions