如何使用Mongoose将文档(记录)插入MongoDB?
我的当前尝试:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var notificationsSchema = mongoose.Schema({
"datetime" : {
type: Date,
default: Date.now
},
"ownerId":{
type:String
},
"customerId" : {
type:String
},
"title" : {
type:String
},
"message" : {
type:String
}
});
var notifications = module.exports = mongoose.model('notifications', notificationsSchema);
module.exports.saveNotification = function(notificationObj, callback){
//notifications.insert(notificationObj); won't work
//notifications.save(notificationObj); won't work
notifications.create(notificationObj); //work but created duplicated document
}
有没有想法为什么我的插入和保存不起作用?我试过创建,但它插入了两个文档,而不是一个。这很奇怪。
notificationObj
的对象的声明吗? - Iceman