11

我在 MongoDB 中有一些数据,如下所示:

{
    name: "Steve",
    location: {
        city: "Nowhere, IL",
        country: "The United States of Awesome"
    }
}

我正在使用对象来组织常见的数据结构(如位置),在 Mongoose 中可能很好地映射到模式。不幸的是,它们似乎并没有真正在 Mongoose 中工作。

如果我只是嵌入一个对象,像这样:

{
    name: String,
    location: {
        city: String,
        country: String
    }
}

它似乎工作,但表现出一些奇怪的行为,导致我的问题(例如instance.location.location返回location,和子对象从父模式继承方法)。我在 Mongoose 列表上启动了一个线程,但它没有看到任何动作。

如果我嵌入一个模式,像这样:

{
    name: String,
    location: new Schema({
        city: String,
        country: String
    })
}

…我的应用程序没有启动(Schema不是 Mongoose 支持的类型)。同上

{
    name: String,
    location: Object
}

…无论如何,这并不理想。

我是否遗漏了某些东西,或者我的模式与 Mongoose 不兼容?

4

2 回答 2

3

我做了类似的事情:

var Topic = new Schema({
      author    : ObjectId
    , title     : String
    , body      : String
    , topics    : [Topic]
});

这在我的测试中运行良好。但是,移除阵列支架会导致错误。对我来说似乎是一个错误。

https://github.com/LearnBoost/mongoose/blob/master/lib/mongoose/schema.js#L185

转储类型,我只得到 String、Number、Boolean、DocumentArray、Array、Date、ObjectId、Mixed——这似乎是故意的,schema/index.js 看起来不像是动态地将新模式注册到类型列表中,所以我猜这还不是受支持的用例。

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

“嵌入单个文档是不可能的。这不是一个好主意(只需使用常规嵌套对象)”

乔什

于 2011-05-31T01:17:42.160 回答
1

看起来这是一个错误,它已在 Mongoose 2.0 中修复!

于 2011-09-20T16:14:43.273 回答