在Mongoose / MongoDB中保存嵌套模式的对象。

huangapple go评论71阅读模式
英文:

Saving objects in a nested schema in Mongoose / MongoDB

问题

我是新手使用 MongoDB 和 Mongoose,我有一种感觉我在做什么不对的事情。

假设我在 Mongoose 中有以下模式:

const characterSchema = new Schema({
  name: {
    required: true,
    type: String,
  },
});

const gameSchema = new Schema({
  name: {
    required: true,
    type: String,
  },
  characters: [characterSchema],
});

对我来说,这告诉我我有一个一对多的关系,一个游戏有多个角色。然而,如果我对一个角色执行更新操作,使用 await Character.findOneAndUpdate 然后运行 db.characters.find()db.games.find(),这两个角色的条目将不同。只有一个会有更新。

这告诉我这是数据的两个独立实例... 我不确定为什么我会希望那样做。我更希望更新模型一次,然后在所有地方都反映出来。我是否做错了什么?是否有一种方式可以使用 Character 模型或游戏实例像 game.characters[0] 一样写入数据,只需要更新一次?

感谢您的时间。

英文:

I'm new to using MongoDB and Mongoose and I have a feeling I'm doing something wrong.

Assume I have the following schema in Mongoose:

const characterSchema = new Schema({
  name: {
    required: true,
    type: String,
  },
});

const gameSchema = new Schema({
  name: {
    required: true,
    type: String,
  },
  characters: [characterSchema],
});

To me, this tells me that I have a one to many relationship where a game has many characters. However, if I perform an update to a character with... await Character.findOneAndUpdate and then run db.characters.find() and db.games.find(), the two entries for the character will be different. Only one of them will have the update.

This tells me that these are two separate instances of the data... And I'm not sure why I'd ever want that. I would prefer to update the model once and have it be reflected everywhere. Am I doing something wrong? Is there a way to write with either the Character model or and instance of game like game.characters[0] and only need to update the data once?

Thank you for your time.

答案1

得分: 1

对于这种情况,有两种一般的方法:

首先,更可扩展的方法是创建一个“中介”参考模式,作为游戏和角色之间的一对一关系 --

// GameCharacter 模式
{
    "character": {
        type: mongoose.Types.ObjectID,
        ref: 'characters',
        required: true
    },
    "game": {
        type: mongoose.Types.ObjectID,
        ref: 'games',
        required: true
    }
}

其次,如果您认为给定游戏的角色引用数量将保持相对较少,您可以选择将角色引用保存在游戏对象内,而不是创建一个单独的参考模式 --

// Game 模式
{
    ...
    "characters": [
        {
            type: mongoose.Types.ObjectID,
            ref: 'characters',
            required: true
        }
    ]
}

在任何一种情况下,您都可以使用聚合管道或 populate 从引用中获取嵌套信息。

英文:

For this scenario, there are two general approaches:

First, a more scalable approach would be to create an "intermediary" reference schema, serving as a one-to-one relationship between the game and the character --

// GameCharacter schema
{
    "character" : {
        type: mongoose.Types.ObjectID,
        ref: 'characters',
        required: true
    },
    "game" : {
        type: mongoose.Types.ObjectID,
        ref: 'games',
        required: true
    }
}

Second, if you believe the number of character references for a given game will be kept to a relative minimum, you can opt to save the character references within the game object, rather than creating a separate reference schema --

// Game schema
{
    ...
    "characters" : [
        {
            type: mongoose.Types.ObjectID,
            ref: 'characters',
            required: true
        }
    ]
}

In either scenario, you can use aggregate pipelines or populate to grab the nested information from the references.

huangapple
  • 本文由 发表于 2023年5月7日 02:10:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定