Difference between insert new data using instance of new object or using .create

时间:2017-04-10 00:00:02

标签: javascript node.js mongodb express mongoose

What's the diference between these two ways?

Example:

var user = new User({
    user.name = "Blabla",
    user.password = "blabla"
});


User.create({name:req.body.blabla, password:req.body.blablabla},callback func)

1 个答案:

答案 0 :(得分:0)

The first one is simply instantiating a new user instance before saving to the database.

var user = new User({
  user.name = "Blabla",
  user.password = "blabla"
});

You'll need to save it to the database like this

user.save(callback)

However, the second implementation has save as part of its internal implementation. So, it basically instantiate the User model and save the new instance