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.
Or in other words, upsert is a combination of update and insert (update + insert = upsert). If the value of this option is set to true and the document or documents found that match the specified query, then the update operation will update the matched document or documents.
Here in MongoDB, the upsert option is a Boolean value. Suppose the value is true and the documents match the specified query filter. In that case, the applied update operation will update the documents. If the value is true and no documents match the condition, this option inserts a new document into the collection.
Since upsert is defined as operation that "creates a new document when no document matches the query criteria" there is no place for upserts in insert command.
Collections are Meteor's way of storing persistent data. The special thing about collections in Meteor is that they can be accessed from both the server and the client, making it easy to write view logic without having to write a lot of server code.
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 } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With