Q:

mongoose save a document

// Example for a model called Person with two properties, e.g. name, surname
// Values to be saved
const person = { name: Jane, surname: Doe};
// you can access the _id property from savedPerson variable (savedPerson._id)
savedPerson = await new Person(person).save()
1
const Person = mongoose.model('Person', Schema({
  name: String,
  rank: String
}));

const doc = new Person({
  name: 'Will Riker',
  rank: 'Commander'
});
// Inserts a new document with `name = 'Will Riker'` and
// `rank = 'Commander'`
await doc.save();

const person = await Person.findOne();
person.name; // 'Will Riker'
0

New to Communities?

Join the community