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

为什么我不能在Mongoose Schema键的开头使用$ character

IT培训 admin 8浏览 0评论

为什么我不能在Mongoose Schema键的开头使用$ character

我想在Mongoose Schema中使用$(数据以这种方式从另一个应用程序格式化),如下所示:

const mySchema = new mongoose.Schema({
  $meta1: {
   type: String,
   trim: true,
  },
  $meta2: {
    type: String,
    trim: true,
  },
  normalKey3: {
    type: String,
    trim: true,
  }
});

当我尝试这个,并运行该功能(使用PUT路线):

exports.updateMe = async (req, res) => {
  const xyz = await MySchema.findOneAndUpdate({normalKey3: req.body.normalKey3}, req.body, 
  {
    upsert: true,
    new: true, 
    runValidators: true
  }).exec();
  res.status(200).json(xyz);
};

我收到以下错误。如果我用_替换$(例如_meta1)就行了。这是Mongoose还是Mongo问题? $表示什么不起作用?

错误:

Error: Invalid atomic update value for $meta1. Expected an object, received string
[            
                

为什么我不能在Mongoose Schema键的开头使用$ character

我想在Mongoose Schema中使用$(数据以这种方式从另一个应用程序格式化),如下所示:

const mySchema = new mongoose.Schema({
  $meta1: {
   type: String,
   trim: true,
  },
  $meta2: {
    type: String,
    trim: true,
  },
  normalKey3: {
    type: String,
    trim: true,
  }
});

当我尝试这个,并运行该功能(使用PUT路线):

exports.updateMe = async (req, res) => {
  const xyz = await MySchema.findOneAndUpdate({normalKey3: req.body.normalKey3}, req.body, 
  {
    upsert: true,
    new: true, 
    runValidators: true
  }).exec();
  res.status(200).json(xyz);
};

我收到以下错误。如果我用_替换$(例如_meta1)就行了。这是Mongoose还是Mongo问题? $表示什么不起作用?

错误:

Error: Invalid atomic update value for $meta1. Expected an object, received string
[                
                
            
发布评论

评论列表 (0)

  1. 暂无评论