当定义一个字段两次时,Mongoose抛出`Field不在schema`错误中

时间:2015-02-04 00:07:51

标签: node.js mongodb mongoose database-schema

我使用的是Node v0.10.31和mongoose@3.8.22。

我想我遇到了特定事情发生时出现的错误。这个bug的含义使我无法拥有一个字段" name"和#34; 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来确定: - )

1 个答案:

答案 0 :(得分:1)

这个问题似乎是Mongoose中的一个错误。该问题已修复,将根据以下链接在Mongoose 3.8.24中发布:

https://github.com/LearnBoost/mongoose/issues/2665