How to use Meteor Upsert

meteor upsert example
mongodb upsert
meteor docs
meteor insert callback
meteor findoneandupdate
meteor publish
meteor mongodb connection
meteor methods

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 additional rows inserted, rather than just updating. I spend the past 30 minutes googling, but I can't find any examples I can understand.

Here's my code:

Values.upsert(
    {
      // Selector
      source: "SourceOne",
      currency: "USD"
    },
    {
      // Modifier
      value: res.data['data']['last']['value'],
      time: Date.now(),
    }
  );

I've also tried

if(Values.find(
      {},{fields: {'source':"SourceOne", 'currency': "USD"}}
    )) {
    Values.update(
      {
        source: "SourceOne", 
        currency: "USD"
      },
      {
        value: res.data['data']['last']['value'],
        time: Date.now()
      }
    );
  } else {
    console.log('blah');
    Values.insert({
      source: "SourceOne", 
      currency: "USD",
      value: res.data['data']['last']['value'],
      time: Date.now()
    });
  }

And still can't seem to figure it out.

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
    }
});

How to use Upsert - help, Could someone post a link to a tutorial or blog posting somewhere, with a detailed example of how to use [collection].upsert? Thanks! meteor remove autopublish and instead call Meteor.publish to specify which parts of your collection should be published to which users. // Create a collection called `Posts` and put a document in it. The document // will be immediately visible in the local copy of the collection.

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
  }
}
);

Collections, You can use update to perform a Mongo upsert by setting the upsert option to true. You can also use the upsert method to perform an upsert that returns the _id of the document that was inserted (if there was one) in addition to the number of affected documents. aldeed/meteor-autoform meteor-autoform - AutoForm is a Meteor package that adds UI components and helpers to easily create basic forms with automatic insert and update events, and automatic reactive validation. Change the type attribute’s value to “insert” or “update” as appropriate, probably by updating a reactive variable.

Try this:

values.update({"id":id},
       { $set: {
          value: res.data['data']['last']['value'],
          time: Date.now() // no need coma here
      } },
      { upsert: true }
    );

Creating upsert MongoDB queries, Through MongoDB, Meteor removes the burden of checking by allowing you to upsert (update + insert) records using a simple syntax. This recipe will show you​  Elaborating more (and guessing more deeply), when you use a Collection, then the api is right, because we return knownId from the mod._id or selector._id (query basic) or no ID at all. But internally when the upsert will use update or insert, it will get a representation of the ID it does not know how to use.

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

Meteor upsert example, A simple example on how to use the upsert query with Meteor framework. Values.​upsert({ // Selector source: "SourceOne", currency: "USD" }  Mongo 2.4 had the following behavior: if c.update({_id: 'x'}, {y: 1}, {upsert: 1}) resulted in an insert, it would get a random _id instead of 'x'. (This only happened when the update was a "replacement" rather than a "modifier".) We dutifully implemented this behavior in various parts of Meteor and added tests to prove that it occured.

Mongo Upsert _id tracking : Meteor, I know this behavior default in Meteor follows Mongo and there is no way of tracking the updated row as if you were using a Collection.insert function but what  Use of upsert with an external ID can reduce the number of DML statements in your code, and help you to avoid hitting governor limits (see Execution Governors and Limits). This next example uses upsert and an external ID field Line_Item_Id__c on the Asset object to maintain a one-to-one relationship between an asset and an opportunity line item.

[Meteor 1.4.1.1] Mongo Upsert with multi-attribute query results in , Environment: Meteor 1.4.1.1 OS: MacOSX 10.11.6 Dependencies: But internally when the upsert will use update or insert, it will get a  If you need your bulk loaded docs to use the Meteor ‘STRING’ format you can generate the _id yourself for each doc using Random.id (see http://docs.meteor.com/packages/random.html#Random-id) and include it in doc object before using it.

Meteor-Community-Packages/meteor-collection-hooks , find hooks are also fired when fetching documents for update , upsert and remove hooks. If using the direct version to bypass a hook, any mongo operations done  The Meteor Accounts system builds on top of the userIdsupport inpublishand methods. The corepackages add the concept of user documents stored in the database, andadditional packages add secure passwordauthentication, integration with third partylogin services, and a pre-built userinterface. The basic Accounts system is in the accounts-basepackage, butapplications typically include this automatically by adding one of thelogin provider packages: accounts-password,

Comments
  • Figured it out through trial and error... posted solution above
  • You should always post answer material as answers in the boxes below.
  • @MikeGraf I wasn't able to when I first joined Stack since I didn't have enough reputation to post answers or something. I've done so now
  • +1 for the // Selector and // Modifier comments. Until I saw those I didn't really understand the concepts here...
  • Thanks for this! Sure wish the official documentation gave examples.
  • will it also insert source and currencywhen there is no document to update?