Reputation: 53
I'm trying to delete an Object inside an array, I have tried both pull and unset in Meteor.update but the entire array is being deleted instead of just the individual object inside the array.
Code for what I've tried
Cars.update(
{ _id: id},
{ $unset: { 'models': { '$.id': modelId } } })
And
Cars.update(
{ _id: id},
{ $pull: { 'models': { 'id': modelId } } })
In both cases, the entire 'models' attribute was deleted instead of just an object from the array
Schema for Cars:
models : {
type: [Object],
optional: true
},
'models.$.id': {
type: String,
autoValue: function() {
return Meteor.uuid()
},
optional: true
}
Essentially, the collections 'Cars' contains an array 'models' which is an array of objects (car models). Each car model object has an attribute id. I want to delete an individual car model from the models array using the attribute 'id' but my above attempts deleted the entire 'models' array instead of the individual object. Any help would be appreciated. Thanks
Before
{
"_id" : "XxbKzS6GHthxwnLFq",
"createdAt" : ISODate("2018-05-04T08:05:59.151Z"),
"updatedAt" : ISODate("2018-05-04T08:36:11.785Z"),
"models" : [
{
"name" : "Mercedes",
"id" : "9927cfe1-f5ae-4625-b6eb-87868793a229"
},
{
"name" : "BMW",
"id" : "86f24e9d-dd08-4407-b350-63d9b25dc094"
}
]
}
After
{
"_id" : "XxbKzS6GHthxwnLFq",
"createdAt" : ISODate("2018-05-04T08:05:59.151Z"),
"updatedAt" : ISODate("2018-05-04T09:38:56.470Z")
}
The parent object is XxbKz... and it contains an attribute models which is an array of objects (BMW,Mercedes). I want to delete the BMW object from the XxbKz parent object. I queried the parent object using its id (XxbKz...) and the BMW object using its id (86f2...) as well (code in original post). The result was that the entire models array got deleted (both BMW and Mercedes) instead of just BMW.
My Meteor call was
Meteor.call('deleteCar','XxbKzS6GHthxwnLFq','86f24e9d-dd08-4407-b350-
63d9b25dc094')
deleteCar(carId, modelId) {
check(carId, String)
check(modeld, String)
if (Meteor.isServer) {
let car = Cars.update( {_id: carId},
{ $pull: { 'models': { 'id': modelId } } }
)
console.log(car)
return car
}
}
}
The variables in the deleteCar function are carId(XxbK) and modelId(86f2) which are the first and second parameters from the deleteCar meteor call. From my understanding, just the BMW from the parent should have been deleted but for some reason this is not the case
Upvotes: 0
Views: 245
Reputation: 351
This works to remove Mercedes from your example.
db.cars.update({_id: 'XxbKzS6GHthxwnLFq'}, {$pull: {models: {id: '9927cfe1-f5ae-4625-b6eb-87868793a229'}}})
Looks just like
Cars.update(
{ _id: id},
{ $pull: { 'models': { 'id': modelId } } })
I did run this from the mongo shell and not meteor. However I have used $pull from within Meteor no problem.
I have never used the return update, it may well be returning the original document before updating. I would add this instead.
Cars.update( {_id: carId},
{ $pull: { 'models': { 'id': modelId } } }
)
const car = Cars.findOne(carId)
console.log(car)
return car
Good luck and let me know how you get on.
Upvotes: 1