mongoose document code example
Example 1: deleteOne mongoose
Tank.deleteOne({ size: 'large' }, function (err) {
if (err) return handleError(err);
// deleted at most one tank document
});
Example 2: create document mongoose
var Tank = mongoose.model('Tank', yourSchema);
var small = new Tank({ size: 'small' });
small.save(function (err) {
if (err) return handleError(err);
// saved!
});
// or
Tank.create({ size: 'small' }, function (err, small) {
if (err) return handleError(err);
// saved!
});
// or, for inserting large batches of documents
Tank.insertMany([{ size: 'small' }], function(err) {
});
Example 3: mongoose save a document
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'