Reputation: 199
I'm learning MongoDB and Mongoose, and I've watched instructors advise to use the save() function to save the changes made to the database.
But I'm still making changes successfully without using save(). I've made two functions to add a document to the database, one with using save() and the second one without using save(), and they all did the same job.
So, what's the point of using it then?
NOTE: I've hided my connect string
My Code:
const express = require('express');
const mongoose = require('mongoose');
//-- Connect to Database --//:
mongoose.connect();
// make Schema & model:
const Person = mongoose.model('Person', new mongoose.Schema({
name: String,
age: Number
}));
//-- Interact with Database --//:
async function run1 () {
const person = await Person.create({name: 'John', age: 25});
await person.save();
console.log(person);
}
async function run2 () {
const person = await Person.create({name: 'Sally', age: 40});
console.log(person);
}
run1();
run2();
Terminal output:
PS C:\Users\user\OneDrive\Desktop\Folders\Programming\Web Development\Web Development Projects\Database-Practice> node server
{
name: 'Sally',
age: 40,
_id: new ObjectId("61d20d852b1e59a6b21149b1"), __v: 0
}
{
name: 'John',
age: 25,
_id: new ObjectId("61d20d852b1e59a6b21149b0"), __v: 0
}
Picture of my Database after running my code:
Upvotes: 4
Views: 6648
Reputation: 753
Shortcut for saving one or more documents to the database.
MyModel.create(docs)
doesnew MyModel(doc).save()
for every doc in docs.
async function run1 () {
const person = await Person.create({name: 'John', age: 25});
await person.save(); // unnecessary 2nd call here
console.log(person);
}
async function run1 () {
const person = await new Person({name: 'John', age: 25}).save();
await person.save(); // saving twice basically
console.log(person);
}
async function run1 () {
const person = new Person({name: 'John', age: 25});
await person.save();
}
The reason is that doing so gives you more control when saving your document. For example, you can pass the person object to any function and save it inside that function instead without requiring the model to wherever that function exists in your codebase.
async function run1 () {
const person = new Person({name: 'John', age: 25});
await doSth(person); // showing possibility, not a recommendation
}
// This function can exist anywhere, does not have to be in the same file
async doSth(person){
// do some logging maybe
// change some properties of the person maybe
// slug-ify the persons name maybe
await person.save();
}
Doing this allows you to have a better separation of concern. Other than that, both will produce the same behavior and is pretty much a matter of preference. But the current standard in the community goes with the .save()
method.
Upvotes: 8