1

i need a confirmation for module.create() using mongoose into NodeJS:

//import my model that defined a Schema and it insert into Todos model
var Todos = require("../models/todoModel");

function setupCtrl(app) {

app.get("/api/setupTodos", function (req, res) {

    //create an array of documents
    var listTodos = [
        {
            username: "test",
            todo: "Buy milk",
            isDone: true,
            hasAttachment: false
        },
        {
            username: "test",
            todo: "Feed dog",
            isDone: false,
            hasAttachment: false
        },
        {
            username: "test",
            todo: "Learn Node",
            isDone: false,
            hasAttachment: false
        }
    ];

    **Todos.create(listTodos**, function (err, results) {

        res.send(results);
    });

When i use Todos.create()( Todo is my model ), i insert into my MongoDB an collection "listTodos"? Why i use this method if i create an array of documents when i use the save() method for insert a single document into database?

Thanks all

Morris
  • 601
  • 1
  • 8
  • 22
  • Hard to follow what you are asking here. Are you asking why you should use `create()` instead of `save()`? – chridam Oct 03 '16 at 11:50
  • Yes! Usually, when i create one document, i create an object of my model and i use nameDocument.save (). But in this case, i create an array of documents and i can't use save() but i'm forced to use nameModel. create () – Morris Oct 03 '16 at 11:55
  • 2
    Gotcha. I think these questions may help shed some insights http://stackoverflow.com/questions/9305987/, http://stackoverflow.com/questions/38290684/ and http://stackoverflow.com/questions/19701154/ – chridam Oct 03 '16 at 12:10
  • 1
    Clear. "Model.create" does a .save for each document in the array ( if you have an array ), resulting in N database calls (where N is the number of documents in the array). "nameDocument.save()" is the simple method for saving one document into database. Insert this my response into your answer so i can declare it "right" – Morris Oct 03 '16 at 12:41
  • [your answer is here, i hope this help you](http://stackoverflow.com/questions/9305987/nodejs-mongoose-which-approach-is-preferable-to-create-a-document?noredirect=1&lq=1) – Muhamad Eissa Oct 03 '16 at 21:52

0 Answers0