1var Tank = mongoose.model('Tank', yourSchema);
2
3var small = new Tank({ size: 'small' });
4small.save(function (err) {
5 if (err) return handleError(err);
6 // saved!
7});
8
9// or
10
11Tank.create({ size: 'small' }, function (err, small) {
12 if (err) return handleError(err);
13 // saved!
14});
15
16// or, for inserting large batches of documents
17Tank.insertMany([{ size: 'small' }], function(err) {
18
19});
1const Person = mongoose.model('Person', Schema({
2 name: String,
3 rank: String
4}));
5
6const doc = new Person({
7 name: 'Will Riker',
8 rank: 'Commander'
9});
10// Inserts a new document with `name = 'Will Riker'` and
11// `rank = 'Commander'`
12await doc.save();
13
14const person = await Person.findOne();
15person.name; // 'Will Riker'
1// Example for a model called Person with two properties, e.g. name, surname
2// Values to be saved
3const person = { name: Jane, surname: Doe};
4// you can access the _id property from savedPerson variable (savedPerson._id)
5savedPerson = await new Person(person).save()