node.js Mongoose/MongoDB 中嵌套模式的正确模式是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15413630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:52:04  来源:igfitidea点击:

What is the proper pattern for nested schemas in Mongoose/MongoDB?

node.jsmongodbmongoose

提问by jbg

It seems logical to me to do something like the following:

执行以下操作对我来说似乎合乎逻辑:

var AvatarSchema = new Mongoose.Schema({
    type: String,
    url: String
});

var UserSchema = new Mongoose.Schema({
    avatars: [AvatarSchema],
    email: String,
    name: String,
    salt: String,
    token: String
});

var ThinUserSchema = new Mongoose.Schema({
    avatars: [AvatarSchema],
    email: String,
    firstname: String,
    lastname: String,
});

var QuestionSchema = new Mongoose.Schema({
    question: String,
    users: [ThinUserSchema]
});

Then later on. . .do something like the following:

然后后来。. . 执行以下操作:

var question = new Question({
    question: 'Wut?',
    users: users //where users is an array of instances of a user model of the UserSchema
});

Here I would expect the users section of the question to be populated with avatars, emails, firstnames and lastnames. . .however since the users/avatars already have _id, these are not persisted.

在这里,我希望问题的用户部分填充头像、电子邮件、名字和姓氏。. .然而,由于用户/头像已经有 _id,这些不会持久化。

  • Deleting each of the _id from the user/avatars seems silly.
  • Explicitly setting up each user/avatar seems inefficient.
  • Switching to a mixed type, puts EVERYTHING in there, and requires markModified.
  • 从用户/头像中删除每个 _id 似乎很愚蠢。
  • 显式设置每个用户/头像似乎效率低下。
  • 切换到混合类型,将所有东西都放在那里,并且需要 markModified。

What is the proper pattern for these sorts of schemas?

这些模式的正确模式是什么?

Thanks!

谢谢!

回答by Neil

I believe you are correct in your assumptions, it's called Embedded documents in Mongoose, here is the example from the Mongoose documentation.

我相信您的假设是正确的,它在 Mongoose 中称为嵌入式文档,这是来自 Mongoose 文档的示例。

var Comments = new Schema({
    title     : String
  , body      : String
  , date      : Date
});

var BlogPost = new Schema({
    author    : ObjectId
  , title     : String
  , body      : String
  , date      : Date
  , comments  : [Comments]
  , meta      : {
        votes : Number
      , favs  : Number
    }
});

mongoose.model('BlogPost', BlogPost);

Disclaimer: I wouldn't necessarily put the comma before the items!

免责声明:我不一定会将逗号放在项目之前!

回答by Capaj

I am mongoose noob still and if I understand correctly I think what you need to read is this: http://mongoosejs.com/docs/populate.html

我仍然是猫鼬菜鸟,如果我理解正确,我认为您需要阅读的是:http: //mongoosejs.com/docs/populate.html

There is a very nice and simple example, where you have referenced schemas in other schemas. So in order to include a document of particular schema inside another, it is better to include it via reference. When you need it, you call populate on parent document. When you change child document, the populated parent will change as well of course.

有一个非常好的简单示例,您在其中引用了其他模式中的模式。因此,为了将特定架构的文档包含在另一个文档中,最好通过引用包含它。当您需要它时,您可以在父文档上调用 populate。当您更改子文档时,填充的父文档当然也会更改。