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.
You can try this (if you are sure they are the last two):
$popwill 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$pullAllif you want to remove several at once.