Node.js 当字段不是';图式中的t
我有一个模式Node.js 当字段不是';图式中的t,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我有一个模式Dog,strict设置为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 d
Dog
,strict设置为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(...)
我需要在这里对架构中不存在的字段使用点表示法,如何才能做到这一点?在Mongoose模型上设置项的最佳方法是使用model#set。在您的示例中,这将是:
var dog = new Dog();
Dog.set('name', 'Barry');
Dog.set('notInSchema', 'this does work');
dog.save(callback);
看