简体   繁体   中英

Simple mongoose variable update nodejs

I have mongoose schema:

var mongoose = require('mongoose')
var Schema = mongoose.Schema

var myItem = new Schema({
  apple: String
})
var MyItem = mongoose.model('MyItem', myItem)

As you can see, there is only one variable called apple in this model. What I want to do, is every time a method gets called, I want the schema to be updated with a new apple value. Eg the apple value changes constantly, so it needs to overwrite the existing value in the db.

Also... if apple is not already int he db (ie code run first time), I want it to insert it.

Here is what I have done:

MyItem.findOneAndUpdate({apple: apple}, {apple: apple}, {upsert: true}, function (error, updatedApple) {
    if (error) {
      console.log('Error')
    }
    console.log(updated)
  })

However this gives me the following results:

> db.myitems.find()
{ "_id" : ObjectId("578629399a18c920d5e1a988"), "apple" : NaN, "__v" : 0 }
{ "_id" : ObjectId("578629469a18c920d5e1a989"), "apple" : 0, "__v" : 0 }

HOW IT SHOULD WORK: A new entry is only added is the apple has a different value. I want it just to update the same entry - so there will always be one.

So, for example when you do the following > db.myitems.find() you will onyl see one documented an not two. So you would either see

{ "_id" : ObjectId("578629399a18c920d5e1a988"), "apple" : NaN, "__v" : 0 }

or you would either see this:

 { "_id" : ObjectId("578629469a18c920d5e1a989"), "apple" : 0, "__v" : 0 }

Whereas I get both of them back, rather than just updating it.

I also tried adding it {new: true} into the options, however this just returns a 'null' object all the time

Ok, so I may be missing something but in case I'm not I'll expand on my comment.

It appears that you're always setting the filter and the update documents to the same values. That means that you will only add records. Here's what I mean:

var apple = 0;
MyItem.findOneAndUpdate({apple: apple}, {apple: apple}, {upsert: true}, function (error, updatedApple) {
    if (error) {
      console.log('Error')
    }
    console.log(updated)
  })

Given that apple is 0, you're going to try and find a record where apple is 0, and then either update the record such that apple equals 0 (if there is one; so this is no change), or else you'll insert a new record where apple equals 0.

From your example code, I would expect you to actually be trying to increment apple, in which case the update document (the second argument) would need to be {$inc : { apple : 1 }} .

Otherwise, you need to maintain two variables; one for the apple value you're searching for and one for the apple value that you want (which, in practice might be the same value, but you don't know a priori that they will be).

In your db entry examples, I'd do something like this (untested): var oldApple = NaN; // or however you get your search value var newApple = 0;

MyItem.findOneAndUpdate({apple: oldApple}, {apple: newApple}, {upsert: true}, function (error, updatedApple) {
    if (error) {
      console.log('Error')
    }
    console.log(updated)
  })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM