最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何通过id将新对象添加到mongoDB集合中?

IT培训 admin 5浏览 0评论

如何通过id将新对象添加到mongoDB集合中?

我有一个名为Users的集合,用于存储用户的消息和信息。我想通过它的id将新对象添加到现有集合中。

我收到一个错误'TypeError:user.insert不是一个函数' - 我想我错过了一些东西....

这是控制器的方法:

UserDataController.prototype.sendMsg = function (userID, messages_data, cb) {

if (userID) { 

    user.findOne({_id: userID},function(err, result){ //Object id=59e5e542a5ba05056c57d847

        // insert to the collection with the object id

        user.insert({_id: userID} ,messages_data,function(err, result){
            if(err) return cb(err);
            return cb(null, result);
        });
    });

  }

 };

这是我希望获得的结果:

"sentmessages" : [{
    "message_body" : "whatsup", // new object
    "message_subject" : "whatsup",
    "message_date" : "20/01/2017"
  },
  {
   "message_body" : "whatsup", // new object
    "message_subject" : "whatsup",
    "message_date" : "20/01/2017"
  }]

架构看起来像这样:

var sentmessages = new schema({
   message_date: String,
   message_subject : String,
   message_body : String,

});

var UserSchema = new schema({
  firstname: String,
  lastname: String,
  email: String,
  sentmessages :[sentmessages] // here is were i want to add new objects
});
回答如下:

得到它...需要使用$ push

UserDataController.prototype.sendMsg = function (userID, messages_data, cb) 
{

if (userID) {
    var message_fixesd_data = messages_data.sent_messages[0];
        user.update({_id: userID},
            { $push: { sent_messages: {
                message_body : message_fixesd_data.message_body,
                message_subject: message_fixesd_data.message_subject,
                message_date : message_fixesd_data.message_date
             }
        }

            }, function (err, result) {
            if(err)
            {
                return cb(err);
            }
            else
            {

                return cb(true, 'File was save successfully', result);
            }
        });
   }

};

如何通过id将新对象添加到mongoDB集合中?

我有一个名为Users的集合,用于存储用户的消息和信息。我想通过它的id将新对象添加到现有集合中。

我收到一个错误'TypeError:user.insert不是一个函数' - 我想我错过了一些东西....

这是控制器的方法:

UserDataController.prototype.sendMsg = function (userID, messages_data, cb) {

if (userID) { 

    user.findOne({_id: userID},function(err, result){ //Object id=59e5e542a5ba05056c57d847

        // insert to the collection with the object id

        user.insert({_id: userID} ,messages_data,function(err, result){
            if(err) return cb(err);
            return cb(null, result);
        });
    });

  }

 };

这是我希望获得的结果:

"sentmessages" : [{
    "message_body" : "whatsup", // new object
    "message_subject" : "whatsup",
    "message_date" : "20/01/2017"
  },
  {
   "message_body" : "whatsup", // new object
    "message_subject" : "whatsup",
    "message_date" : "20/01/2017"
  }]

架构看起来像这样:

var sentmessages = new schema({
   message_date: String,
   message_subject : String,
   message_body : String,

});

var UserSchema = new schema({
  firstname: String,
  lastname: String,
  email: String,
  sentmessages :[sentmessages] // here is were i want to add new objects
});
回答如下:

得到它...需要使用$ push

UserDataController.prototype.sendMsg = function (userID, messages_data, cb) 
{

if (userID) {
    var message_fixesd_data = messages_data.sent_messages[0];
        user.update({_id: userID},
            { $push: { sent_messages: {
                message_body : message_fixesd_data.message_body,
                message_subject: message_fixesd_data.message_subject,
                message_date : message_fixesd_data.message_date
             }
        }

            }, function (err, result) {
            if(err)
            {
                return cb(err);
            }
            else
            {

                return cb(true, 'File was save successfully', result);
            }
        });
   }

};
发布评论

评论列表 (0)

  1. 暂无评论