I've the following (simplified) SimpleSchema schema in my Meteor 1.1.0.2 app:
Tickers.attachSchema(
new SimpleSchema({
entries: {
type: [TickerEntries],
defaultValue: [],
optional: true
}
})
);
TickerEntries = new SimpleSchema({
id: {
type: String,
autoform: {
type: "hidden",
label: false,
readonly: true
},
optional: true,
autoValue: function () {
if (!this.isSet) {
return new Mongo.Collection.ObjectID()._str;
}
}
},
text: {
type: String,
label: 'Text'
}
};
In the database I do have the following entries:
{
"_id" : "ZcEvq9viGQ3uQ3QnT",
"entries" : [
{
"text" : "a",
"id" : "fc29774dadd7b37ee0dc5e3e"
},
{
"text" : "b",
"id" : "8171c4dbcc71052a8c6a38fb"
}
]
}
I'd like to remove one entry in the entries array specified by an ID.
If I execute the following command in the meteor-mongodb-shell it works without problems:
db.Tickers.update({_id:"3TKgHKkGnzgfwqYHY"}, {"$pull":{"entries": {"id":"8171c4dbcc71052a8c6a38fb"}}})
But the problem is, that if I'm going to do the same from within Meteor, it doesn't work. Here's my code:
Tickers.update({id: '3TKgHKkGnzgfwqYHY'}, {$pull: {'entries': {'id': '8171c4dbcc71052a8c6a38fb'}}});
I've also tried the following:
Tickers.update('3TKgHKkGnzgfwqYHY', {$pull: {'entries': {'id': '8171c4dbcc71052a8c6a38fb'}}});
None of these commands give me an error, but they don't remove anything from my document.
Could it be possible, that the $pull
command isn't supported properly or do I have made a mistake somewhere?
Thanks in advance!
EDIT:
I've found the problem, which couldn't be seen in my description, because I've simplified my schema. In my app there's an additional attribute timestamp
in TickerEntries
:
TickerEntries = new SimpleSchema({
id: {
type: String,
optional: true,
autoValue: function () {
if (!this.isSet) {
return new Mongo.Collection.ObjectID()._str;
}
}
},
timestamp: {
type: Date,
label: 'Minute',
optional: true,
autoValue: function () {
if (!this.isSet) { // this check here is necessary!
return new Date();
}
}
},
text: {
type: String,
label: 'Text'
}
});
Thanks to the hint from Kyll, I've created a Meteorpad and found out, that the autovalue
function is causing the problems.
I've now changed the function to the following code:
autoValue: function () {
if (!this.isSet && this.operator !== "$pull") { // this check here is necessary!
return new Date();
}
}
And now it is working. It seems, that returning a autovalue-value in the case of pulling an item/object, it cancels the pull operation, as the value isn't set to the returned value (so the timestamp attribute keeps the old value but isn't pulled).
Here's the according Meteorpad to test it (simply comment out the check for the operator in the autovalue function): http://meteorpad.com/pad/LLC3qeph66pAEFsrB/Leaderboard
Thank you all for your help, all your posts were very helpful to me!
See Question&Answers more detail:
os