2016-10-07 7 views
0

Я получаю сообщение об ошибке MongoError: The dollar ($) prefixed field '$inc' in '$inc' is not valid for storage.Метеор MongoDB Update Document

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; 

Что я делаю неправильно? Может кто-нибудь мне помочь?

EDIT: Моя коллекция пользователей выглядит следующим образом:

{ 
    "_id": "xxxxx", 
    "username": "xxxx", 
    "hearts": { 
    "counter": 0, 
    "records": [{ 
     "owner": "xxxxx", 
     "expDate": Date 
    }, { 
     "owner": "xxxxx", 
     "expDate": Date 
    }] 
    } 
} 
+0

Да, я уверен, цикл через массив 'hearts.records' документа и, если' hearts.records [индекс] .expDate' 30 дней над ' new Date() 'затем обновить' $ inc hearts.counter' на 1 и '$ set hearts.records [index] .expDate' на новый' expDate' .. Надеюсь, что было ясно – cocacrave

+0

Попробуйте положить точку в другое свойство 'hearts.records. $ {i} .expDate' – chridam

+0

Я попробовал это сейчас. Еще одна ошибка – cocacrave

ответ

0

Я думаю, что так, как вы делаете это, $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);