引用Mongo / mongoose整个文档,包括ObjectId

时间:2017-06-30 07:41:11

标签: mongodb mongoose mongoose-schema

对于重构项目,我正在使用具有以下集合的MongoDB:

Games
Teams
Players
Goals

游戏引用两个团队,一个团队引用两个玩家。两者都有一些单独的属性(比如球员elo和球队elo,还有更多,但我为了简洁而删除了它们。)

我创建了以下mongoose模式并将它们放在模型中:

const playerSchema = new mongoose.Schema({
  name: String,
  elo: Number,
  joinDate: Date
});

const teamSchema = new mongoose.Schema({
  players: [
    { type: Schema.Types.ObjectId, ref: 'Player' }
  ],
  elo: Number,
});

const goalSchema = new mongoose.Schema({
  player: { type: Schema.Types.ObjectId, ref: 'Player' },
  position: String,
  color: String,
  own: Boolean,
  teamId: { type: Schema.Types.ObjectId, ref: 'Team' }
});

const gameSchema = new mongoose.Schema({
  teamRed: ???,
  teamBlue: ???,
  winner: ???,
  teamRedScore: Number,
  teamBlueScore: Number,
  startDate: Date,
  endDate: Date,
  eloChange: Number

});

数据库中的玩家,团队和目标对象是可以理解的

{
    "_id" : "QgxP7BvnG2yxaHCPu",
    "name" : "Rachid",
    "elo" : 1447,
    "joinDate" : ISODate("2016-08-05T11:28:41.772Z")
}

{
    "_id" : "6hEZyWLuP9JHJAuj8",
    "players" : [
        "QgxP7BvnG2yxaHCPu",
        "ZkR2XEJAbXxPgiuTy"
    ],
    "teamElo" : 1631
}

{
    "_id" : "25Myqjwv5qvRBmiex",
    "player" : "QgxP7BvnG2yxaHCPu",
    "position" : "attacker",
    "color" : "red",
    "own" : false,
    "teamId" : "6hEZyWLuP9JHJAuj8"
}

但Game对象似乎重复了其他集合中的一些数据。

{
    "_id" : "3NPo4hnq5LraBw2tZ",
    "teamRed" : {
        "_id" : "B9C3egFE4xbeKMhmW",
        "attacker" : {
            "_id" : "Bv8uDe9wSc39G3Cm6",
            "goals" : 2
        },
        "defender" : {
            "_id" : "DaKzJbzsTYtyhpwP9",
            "goals" : 2
        },
        "goals" : [
            "MW6ioYo7CCEPxLKvt",
            "YsY8cBrEsyR8CWPvW",
            "CHo9agRX8miXBysZC",
            "ct7pziiLNLcq9n3jB"
        ]
    },
    "teamBlue" : {
        "_id" : "FrjJeyT8eiKKEQj7Y",
        "attacker" : {
            "_id" : "xs2B4d3peMmTvLasp",
            "goals" : 5
        },
        "defender" : {
            "_id" : "df6kaePnj7JNAsL62",
            "goals" : 2
        },
        "goals" : [
            "h5DtWDHFoN6bJXnos",
            "cW95gPDBKuXogx3Ba",
            "dLfTKESN4FpmncQXR",
            "2dDAmPARxbtmpYDyL",
            "YaosZP7fDbJjZaACP",
            "AYrWdHchTunPusJft",
            "CZMceQ2vYBqh59JaP"
        ]
    },
    "teamRedScore" : 4,
    "teamBlueScore" : 7,
    "startDate" : ISODate("2016-10-07T15:38:08.637Z"),
    "winner" : {
        "_id" : "FrjJeyT8eiKKEQj7Y",
        "attacker" : {
            "_id" : "xs2B4d3peMmTvLasp",
            "goals" : 5
        },
        "defender" : {
            "_id" : "df6kaePnj7JNAsL62",
            "goals" : 2
        },
        "goals" : [
            "h5DtWDHFoN6bJXnos",
            "cW95gPDBKuXogx3Ba",
            "dLfTKESN4FpmncQXR",
            "2dDAmPARxbtmpYDyL",
            "YaosZP7fDbJjZaACP",
            "AYrWdHchTunPusJft",
            "CZMceQ2vYBqh59JaP"
        ]
    },
    "endDate" : ISODate("2016-10-07T15:44:13.850Z"),
    "eloChange" : 13
}

我真的很困惑如何查询这个集合。我试图用游戏中团队对象的Schema.Types.Mixed类型来做,但似乎是不匹配的,特别是当我不得不稍后插入数据时。 通过ObjectId引用也不起作用,因为虽然ObjectId在数据中,但我似乎无法访问它。此外,Game对象中显示的团队与团队中的团队不同,就像在团队集合中一样。

是否有处理这类情况的一般方法?我应该制作一个新的团队游戏架构吗?

0 个答案:

没有答案