sql >> Base de Datos >  >> NoSQL >> MongoDB

mangosta guardar vs insertar vs crear

El .save() es un método de instancia del modelo, mientras que .create() se llama directamente desde el Model como una llamada de método, siendo de naturaleza estática, y toma el objeto como primer parámetro.

var mongoose = require('mongoose');

var notificationSchema = mongoose.Schema({
    "datetime" : {
        type: Date,
        default: Date.now
    },
    "ownerId":{
        type:String
    },
    "customerId" : {
        type:String
    },
    "title" : {
        type:String
    },
    "message" : {
        type:String
    }
});

var Notification = mongoose.model('Notification', notificationsSchema);


function saveNotification1(data) {
    var notification = new Notification(data);
    notification.save(function (err) {
        if (err) return handleError(err);
        // saved!
    })
}

function saveNotification2(data) {
    Notification.create(data, function (err, small) {
    if (err) return handleError(err);
    // saved!
    })
}

Exporte las funciones que desee fuera.

Más en Mongoose Docs, o considere leer la referencia del Model prototipo en Mongoose.