Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongoose findoneandupdate

// note: this uses async/await so it assumes the whole thing 
// is in an async function 

const doc = await CharacterModel.findOneAndUpdate(
  { name: 'Jon Snow' },
  { title: 'King in the North' },
  // If `new` isn't true, `findOneAndUpdate()` will return the
  // document as it was _before_ it was updated.
  { new: true }
);

doc.title; // "King in the North"
Comment

findOneAndUpdate mongoose

const Character = mongoose.model('Character', new mongoose.Schema({
  name: String,
  age: Number
}));

await Character.create({ name: 'Jean-Luc Picard' });

const filter = { name: 'Jean-Luc Picard' };
const update = { age: 59 };

// `doc` is the document _before_ `update` was applied
let doc = await Character.findOneAndUpdate(filter, update);
doc.name; // 'Jean-Luc Picard'
doc.age; // undefined

doc = await Character.findOne(filter);
doc.age; // 59
Comment

findone and update mongoose

// Using queries with promise chaining
Model.findOne({ name: 'Mr. Anderson' }).
  then(doc => Model.updateOne({ _id: doc._id }, { name: 'Neo' })).
  then(() => Model.findOne({ name: 'Neo' })).
  then(doc => console.log(doc.name)); // 'Neo'

// Using queries with async/await
const doc = await Model.findOne({ name: 'Neo' });
console.log(doc.name); // 'Neo'
Comment

PREVIOUS NEXT
Code Example
Javascript :: how manipulate the multiple input option data in one function in vue js 
Javascript :: Javascript number Count up 
Javascript :: Get the Last Items in an Array 
Javascript :: md5 checksum javascript 
Javascript :: how to get variable from url in javascript 
Javascript :: javascript read word document 
Javascript :: check if array is empty javascript 
Javascript :: delete node from linked list 
Javascript :: node js split 
Javascript :: How to return arguments in an array in javascript 
Javascript :: mariadb JSON_ARRAYAGG does not exist 
Javascript :: javascript string add new line 
Javascript :: how to load js in vuejs components 
Javascript :: selected dropdown value 
Javascript :: js get current seconds 
Javascript :: js append zeros 
Javascript :: remove duplicates javascript 
Javascript :: how to redirect to another page after clicking ok in alert 
Javascript :: javascript best way to loop through array 
Javascript :: how to pass sequelize transaction to save method 
Javascript :: Promise.all() with async and await 
Javascript :: add one month to date javascript 
Javascript :: create or update in sequelize 
Javascript :: try catch js 
Javascript :: js slice string at word 
Javascript :: jquery toastr 
Javascript :: insert isValidPhoneNumber in react hook form 
Javascript :: arithmetic operators in javascript 
Javascript :: how to retrieve the list value of json file in python 
Javascript :: object promise javascript 
ADD CONTENT
Topic
Content
Source link
Name
3+2 =