Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove from array[ObjectId] #361

Closed
gnakor opened this issue May 24, 2011 · 10 comments
Closed

Remove from array[ObjectId] #361

gnakor opened this issue May 24, 2011 · 10 comments

Comments

@gnakor
Copy link

gnakor commented May 24, 2011

Hello,

How can I remove a ObjectId from a array of ObjectId ?

My model :

var User = new Schema({
    title       : { type : String,  index: true },
    name        : String,
    friends     : [ObjectId],
    join_at     : {type:Date, default:Date.now },
    save_at     : {type:Date, default:Date.now }
});

The function for deleting the objectId : uid

User.findOne({'_id' : self.id}, function(err, me){
    for(var i=0; i<=me.friends.length; i++){
        if (String(me.friends[i])==String(uid)){
            me.friends.splice(i, 1);
            break;                          
        }
    }
    me.save(function(err,us){
        next(err,'kljlmjk'+JSON.stringify(me));
    });     
});                                     

Thanks for your response.

@bnoguchi
Copy link
Contributor

bnoguchi commented Jun 8, 2011

Don't use splice.

me.friends.remove(uid);
me.save(callback);

@bnoguchi bnoguchi closed this as completed Jun 8, 2011
@jamlfy
Copy link

jamlfy commented Feb 18, 2013

That function is beautiful!

@nghuuphuoc
Copy link

Is there a way to do this to multiple documents as following?

User.update({
    friends: uid
}, {
    '$pull': {
        friends: uid
    }
})

@jhickner
Copy link

Looking for an answer to this same question (above). How do you do this for multiple documents?

@vkarpov15
Copy link
Collaborator

@jhickner try $pullAll. http://docs.mongodb.org/manual/reference/operator/update/pullAll/

@anshupitlia
Copy link

wonderful

@makuc
Copy link

makuc commented Dec 13, 2017

@bnoguchi Hi, I know this is an old thread, but it is the first post on google (and I did find a solution here).
Just a quick question, though, for completeness sake. Which usage is preferred:

me.friend.remove(uid);
me.save(callback);

Or:

me.friends.pull(uid);
me.save(callback);

Also, is there any more correct way of inserting new "friends" instead of (in short, better alternative for push()):

me.friend.push(uid);
me.save(callback);

Thanks.

@vkarpov15
Copy link
Collaborator

Re: first question, doesn't matter, up to personal preference

Re: 2nd question, better in what way?

@makuc
Copy link

makuc commented Dec 19, 2017

As-in more efficient/faster.
Also, I noticed that .pop(...) can have problems when emptying an array, throwing "cannot save undefined" field after calling the me.save(callback) function, while remove() works ok - hence me asking.

@vkarpov15
Copy link
Collaborator

No, push() should be as fast as you can get while still using save(). You can also do me.update({}, { $push: { friend: uid } }).then(), that will be marginally faster by bypassing middleware, validation, etc. but it won't improve overall latency too much.

Re: the pop() problems, can you open up a separate issue with a stack trace and a script that reproduces this issue please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants