question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Remove from array[ObjectId]

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:10 (1 by maintainers)

github_iconTop GitHub Comments

44reactions
bnoguchicommented, Jun 8, 2011

Don’t use splice.

me.friends.remove(uid);
me.save(callback);
13reactions
nghuuphuoccommented, Nov 26, 2013

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

User.update({
    friends: uid
}, {
    '$pull': {
        friends: uid
    }
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

remove objects from array by object property - Stack Overflow
You can remove an item by one of its properties without using any 3rd party libs like this: var removeIndex = array.map(item =>...
Read more >
Remove Object from an Array by its Value in JavaScript
Remove an Object from an Array by its Value using Array.slice() # ... To remove an object from an array by its value:...
Read more >
Remove an Element from an Array by ID in JavaScript
Learn how to easily remove an object with a specified ID from an array in JavaScript.
Read more >
Remove object from array of objects in Javascript - gists · GitHub
we have an array of objects, we want to remove one object using only the id property. const apps = [{id:34,name:'My App',another:'thing'},{id:37,name:'My ...
Read more >
Removing Items from an Array in JavaScript - Ultimate Courses
To remove an item from array via its index, we'll first introduce the Array.prototype.splice method and then investigate a better pattern using ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found