【问题标题】:Mongoose use dot notation when fields aren't in schema当字段不在模式中时,Mongoose 使用点表示法
【发布时间】:2013-05-01 00:33:10
【问题描述】:

我有一个 Dog 架构,其严格设置为 false,允许保存架构中不存在的值。但是,我似乎无法使用点符号来设置如下值:

var dog = new Dog();
dog.name = "Barry"; // works because name is in schema
dog.notInSchema = "This should work!" // doesn't work because its not in the schema
dog.save(...)

这样的事情确实有效:

var dog = new Dog({name : "kyle", notInSChema : "This works and saves" })
dog.save(...)

我需要在此处对架构中没有的字段使用点表示法,我该怎么做?

【问题讨论】:

    标签: node.js mongodb express mongoose


    【解决方案1】:

    在 Mongoose 模型上设置项目的最佳方法是使用 Model#set。在您的示例中,这将是:

    var dog = new Dog();
    Dog.set('name', 'Barry');
    Dog.set('notInSchema', 'this does work');
    dog.save(callback);
    

    http://mongoosejs.com/docs/api.html#document_Document-set

    【讨论】:

      猜你喜欢
      • 2015-09-30
      • 2022-01-21
      • 2019-05-13
      • 2015-04-03
      • 2019-01-25
      • 1970-01-01
      • 2019-11-06
      • 2018-10-22
      • 1970-01-01
      相关资源
      最近更新 更多