MongoDB $pull value from array of ObjectIDs
mongodb, mongoose
Solution
Your current schema doesn't provide any direction to Mongoose regarding the data type contained within the `owners` array field. If you want Mongoose to cast your string to an ObjectID you need to provide type information in your schema:
var BusinessSchema = new Schema({
business_name: {type: String, required: true},
owners: [{type: Schema.ObjectId}]
});
Problem
I have this document in my collection: ``` { "_id" : ObjectId("52718433e18a711923000005"), "owners" : [ ObjectId("52718433e18a711923000004"), ObjectId("52ed40dccc5bc50000000003"), ObjectId("52ed4171abe2780000000003") ] } ``` I have the following statement, where I am trying to remove one of the values in `owners` field: ``` Business.update({_id:req.body._id}, {$pull:{"owners":req.body.userid}}, function(err){ if(err){ res.json(500, {message:"Could not remove user from admin list"}); }else{ res.json(200); } }); ``` I know that `req.body._id` and `req.body.userid` have valid values: ``` { _id: '52718433e18a711923000005', userid: '52ed4171abe2780000000003' } ``` Other operations, such as finding business by ID, etc, work, so it's not an ObjectId format issue. What else could it be? -- Edit: here is my (abbreviated) schema definition: ``` var BusinessSchema = new Schema({ business_name: {type: String, required: true}, owners: {type: Array} }); ```