How to use Meteor Upsert

前端 未结 4 762
悲哀的现实
悲哀的现实 2021-02-02 07:57

Having a bit of trouble getting my Meteor upsert function working. I am fairly (200 lines of code) new, and I\'m having a bit of trouble.

The collection keeps on having

相关标签:
4条回答
  • 2021-02-02 08:31

    Mongo.Collection#upsert(selector, modifier, [options], [callback])

    ARGUMENTS

    selector: Mongo Selector, Object ID, or String Specifies which documents to modify

    modifier: Mongo Modifier Specifies how to modify the documents

    callback: Function Optional. If present, called with an error object as the first argument and, if no error, the number of affected documents as the second.

    OPTIONS: multi Boolean True to modify all matching documents; false to only modify one of the matching documents (the default).

    https://docs.meteor.com/api/collections.html#Mongo-Collection-upsert

    0 讨论(0)
  • 2021-02-02 08:38

    Try this:

    values.update({"id":id},
           { $set: {
              value: res.data['data']['last']['value'],
              time: Date.now() // no need coma here
          } },
          { upsert: true }
        );
    
    0 讨论(0)
  • 2021-02-02 08:45

    Figured it out through trial and error:

    Values.upsert({
        // Selector
        source: "SourceOne",
        currency: "USD"
    }, {
        // Modifier
        $set: {
            value: res.data['data']['last']['value'],
            time: Date.now() // no comma needed here
        }
    });
    
    0 讨论(0)
  • 2021-02-02 08:47

    The above doesn't work for IDs. This works for me (with the same syntax as update):

    Values.upsert(id,
    {
      // Modifier
      $set: {
          value: res.data['data']['last']['value'],
          time: Date.now() // no need coma here
      }
    }
    );
    
    0 讨论(0)
提交回复
热议问题