Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 检查文档中的字段_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Node.js 检查文档中的字段

Node.js 检查文档中的字段,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,假设我有以下模式 var userSchema = new Schema({ name : String }); var User = mongoose.model('User',userSchema); 编辑:若用户试图更新字段,但该字段不存在,我需要抛出异常。我的问题是如何检查更新文档中是否不存在更新字段。以下是我需要的一个小例子: app.post('/user/update/:id', function (req, res) { var field =

假设我有以下模式

 var userSchema = new Schema({
    name : String
  });

  var User = mongoose.model('User',userSchema);
编辑:若用户试图更新字段,但该字段不存在,我需要抛出异常。我的问题是如何检查更新文档中是否不存在更新字段。以下是我需要的一个小例子:

  app.post('/user/update/:id', function (req, res) {
     var field = req.param('field'),
          value = req.param('value'),
          id = req.param('id');

     User.findOne({_id: id},function(err, user){
        if(err) throw err;

        if (user) {

          user[field] = value;          // Here is I need to check that field is exists
                                        // in user schema. If does't I have to throw 
                                        // an execption.

          user.save(function (err){
             return res.send(200);
          });
        }            
     })
  });
尝试将$exists添加到update的查询参数中。这将允许您仅在某个字段存在或不存在时更新文档


从Mongoose v3.1.2指南:

默认情况下启用的strict选项可确保添加到模型实例中的、未在模式中指定的值不会保存到数据库中。注意:除非有充分的理由,否则不要设置为false

strict选项也可以设置为throw,这将导致产生错误,而不是忽略坏数据

在上面的示例中,当我更新一个不存在的字段时,没有出现错误。

您可以使用.schema.path检查该字段是否存在于架构中。在您的特定用例中,您可以执行以下操作:

app.post('/user/update/:id', function (req, res) {
 var field = req.param('field'),
      value = req.param('value'),
      id = req.param('id');

  User.findOne({_id: id},function(err, user){
    if(err) throw err;

    if (user) {

      if(User.schema.path(field)) {
        user[field] = value;
      } else {
        throw new Error('Field [' + field + '] does not exists.');
      }

      user.save(function (err){
        return res.send(200);
      });
    }            
  });
});

我不清楚你的问题是什么,因为他没有问任何问题,他只是说明了他做了什么。我已经补充了关于我的问题的更多信息。谢谢你的回答,但如果没有,我需要一笔费用。@Erik-对,如果不存在,然后,如果您使用的是安全模式,您可以使用$exists检查更新的结果,以查看nupdated=0,然后在那里抛出异常。
app.post('/user/update/:id', function (req, res) {
 var field = req.param('field'),
      value = req.param('value'),
      id = req.param('id');

  User.findOne({_id: id},function(err, user){
    if(err) throw err;

    if (user) {

      if(User.schema.path(field)) {
        user[field] = value;
      } else {
        throw new Error('Field [' + field + '] does not exists.');
      }

      user.save(function (err){
        return res.send(200);
      });
    }            
  });
});