#mongodb #meteor
#mongodb #метеор
Вопрос:
Я получаю сообщение об ошибке MongoError: The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage.
const modifier = { $set: {}, $inc: { 'hearts.counter': 1 } };
modifier.$set[`hearts.records${i}.expDate`] = expDate;
Meteor.users.update(lookUpUser._id, { modifier });
До сих пор я пытался:
Meteor.users.update(lookUpUser._id, modifier);
и
const modifier = { $set: {}, $inc: {} };
modifier.$inc['hearts.counter'] = 1;
Что я делаю не так? Кто-нибудь может мне помочь, пожалуйста?
РЕДАКТИРОВАТЬ: моя коллекция пользователей выглядит следующим образом:
{
"_id": "xxxxx",
"username": "xxxx",
"hearts": {
"counter": 0,
"records": [{
"owner": "xxxxx",
"expDate": Date
}, {
"owner": "xxxxx",
"expDate": Date
}]
}
}
Комментарии:
1. Да, я просматриваю массив документа
hearts.records
, и еслиhearts.records[index].expDate
прошло 30 днейnew Date()
, то обновите$inc hearts.counter
на 1 и$set hearts.records[index].expDate
до новогоexpDate
.. Надеюсь, это было ясно2. Попробуйте поставить точку в другом свойстве
hearts.records.${i}.expDate
3. Я только что попробовал. Все еще получаю ту же ошибку
Ответ №1:
Я думаю, что то, как вы это делаете, $set
представляет собой массив, а не объект. И, как упоминал @chridam, вам, вероятно, нужно добавить точку. Попробуйте это:
const modifier = { $set: {}, $inc: { 'hearts.counter': 1 } };
modifier.$set = { [`hearts.records.${i}.expDate`]: expDate }
Meteor.users.update(lookUpUser._id, modifier);
или напрямую:
const modifier = {
$set: {
[`hearts.records.${i}.expDate`]: expDate,
},
$inc: {
'hearts.counter': 1,
},
};
Meteor.users.update(lookUpUser._id, modifier);