Reputation: 75854
mongo> db.users.findOne({username: 'foo'});
{
"__v" : 266,
"_id" : ObjectId("50752b5f00a0f5ab64000002"),
"followers" : [
ObjectId("505e2f1de888c7d701000001"),
ObjectId("506146fe72c0280723000001"),
ObjectId("50752b5f00a0f5ab64000002"), //remove this
ObjectId("50752b5f00a0f5ab64000002") //remove this
],
I want to remove the last two items in followers[] collection.
Upvotes: 0
Views: 229
Reputation: 15712
You can try this (if you are sure they are the last two):
db.users.update({"_id" : ObjectId("50752b5f00a0f5ab64000002")}, {$pop: {followers:1}});
db.users.update({"_id" : ObjectId("50752b5f00a0f5ab64000002")}, {$pop: {followers:1}});
$pop
will remove the last element of an array.
However, if you need to remove specific ids from the list regardless of where they are, you can use $pull
, or $pullAll
if you want to remove several at once.
Upvotes: 3