Mongoose 在定义一个字段两次时抛出“Field is not in schema”错误
Mongoose throws `Field is not in schema` error when defining a field twice
我正在使用 Node v0.10.31 和 mongoose@3.8.22。
我想我遇到了一个在特定事情发生时出现的错误。此错误的影响使我无法在同一架构上拥有字段 "name" 和 "father.name.full"。
这是我定义架构的方式:
'use strict';
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/myapp');
var PersonSchema = new mongoose.Schema({
name: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Name', // if this is commented out, no errors
required: false,
default: null,
},
father: {
name: { full: String } // if this is `name: String`, no errors
}
}, {
strict: 'throw' // if this is commented out, no errors
});
var Person = mongoose.model('Person', PersonSchema);
这是我创建文档的方式:
var object2save = {
name: mongoose.Types.ObjectId(),
father: {
name: {
full: 'father full name'
}
}
}
var doc = new Person(object2save); // this throws the error
错误和堆栈跟踪是
Error: Field `name` is not in schema.
at model.Document.set (/PATH/node_modules/mongoose/lib/document.js:469:19)
at model.Document.set (/PATH/node_modules/mongoose/lib/document.js:464:16)
at model.Document (/PATH/node_modules/mongoose/lib/document.js:60:10)
at model.Model (/PATH/node_modules/mongoose/lib/model.js:43:12)
at new model (/PATH/node_modules/mongoose/lib/model.js:2535:11)
at Object.<anonymous> (/PATH/Desktop/node/test-mongoose/path-type-mismatch.js:43:11)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
这实际上是一个错误还是一个功能?如果它是一项功能,那么它似乎是对架构设计的限制。我打算在 GitHub 上为 Mongoose 创建一个问题,但我认为我应该先询问 Stack Overflow 以确定:-)
这个问题似乎是 Mongoose 中的一个错误。该问题已修复,将根据此 link:
在 Mongoose 3.8.24 中发布
我正在使用 Node v0.10.31 和 mongoose@3.8.22。
我想我遇到了一个在特定事情发生时出现的错误。此错误的影响使我无法在同一架构上拥有字段 "name" 和 "father.name.full"。
这是我定义架构的方式:
'use strict';
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/myapp');
var PersonSchema = new mongoose.Schema({
name: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Name', // if this is commented out, no errors
required: false,
default: null,
},
father: {
name: { full: String } // if this is `name: String`, no errors
}
}, {
strict: 'throw' // if this is commented out, no errors
});
var Person = mongoose.model('Person', PersonSchema);
这是我创建文档的方式:
var object2save = {
name: mongoose.Types.ObjectId(),
father: {
name: {
full: 'father full name'
}
}
}
var doc = new Person(object2save); // this throws the error
错误和堆栈跟踪是
Error: Field `name` is not in schema.
at model.Document.set (/PATH/node_modules/mongoose/lib/document.js:469:19)
at model.Document.set (/PATH/node_modules/mongoose/lib/document.js:464:16)
at model.Document (/PATH/node_modules/mongoose/lib/document.js:60:10)
at model.Model (/PATH/node_modules/mongoose/lib/model.js:43:12)
at new model (/PATH/node_modules/mongoose/lib/model.js:2535:11)
at Object.<anonymous> (/PATH/Desktop/node/test-mongoose/path-type-mismatch.js:43:11)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
这实际上是一个错误还是一个功能?如果它是一项功能,那么它似乎是对架构设计的限制。我打算在 GitHub 上为 Mongoose 创建一个问题,但我认为我应该先询问 Stack Overflow 以确定:-)
这个问题似乎是 Mongoose 中的一个错误。该问题已修复,将根据此 link:
在 Mongoose 3.8.24 中发布