【发布时间】:2011-09-04 12:51:30
【问题描述】:
我在 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,并且子对象从父模式继承方法)。我在猫鼬名单上started a thread,但它没有看到任何动作。
如果我嵌入一个 Schema,像这样:
{
name: String,
location: new Schema({
city: String,
country: String
})
}
...我的应用程序没有启动(Schema 不是 Mongoose 支持的类型)。同理
{
name: String,
location: Object
}
……无论如何,这并不理想。
我是否遗漏了什么,或者我的架构与 Mongoose 不兼容?
【问题讨论】:
-
在这里查看文档 -> mongoosejs.com/docs/embedded-documents.html.
-
@Andrew 我有。你在给我看什么?我确实注意到它说,“嵌入式文档是具有自己的模式的文档,它们是其他文档的一部分(作为数组中的项目)。”这是否意味着 Mongoose 不支持像这样的模式我的?