admin管理员组文章数量:1313292
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:
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:
Share Improve this question asked Jan 2, 2022 at 20:47 Saud AlghamdiSaud Alghamdi 1993 silver badges10 bronze badges1 Answer
Reset to default 8From the docs:
Shortcut for saving one or more documents to the database.
MyModel.create(docs)
doesnew MyModel(doc).save()
for every doc in docs.
Looking at what you've got, let me point out:
async function run1 () {
const person = await Person.create({name: 'John', age: 25});
await person.save(); // unnecessary 2nd call here
console.log(person);
}
Behind the scenes your code will run like:
async function run1 () {
const person = await new Person({name: 'John', age: 25}).save();
await person.save(); // saving twice basically
console.log(person);
}
What you can do is (this is preferred by most):
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 remendation
}
// 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 munity goes with the .save()
method.
本文标签: javascriptWhat39s the point of using save() in MongooseStack Overflow
版权声明:本文标题:javascript - What's the point of using save() in Mongoose? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741941731a2406181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论