Mongoose - 设置关系在方案中失败

Mongoose - Setting relations failing in scheme

我正在尝试在一个集合中保存一组 ObjectId 以引用另一个集合,如下所示:

var Seasons = new Schema({
    leagues: { type: Schema.ObjectId, ref: 'Leagues', default: null }
})

但是我收到以下错误:

{
    error: {
    stack: "Error at MongooseError.CastError (c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\lib\error\cast.js:18:16) at ObjectId.cast (c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\lib\schema\objectid.js:132:13) at ObjectId.castForQuery (c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\lib\schema\objectid.js:182:17) at Query._castUpdateVal (c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\lib\query.js:2295:17) at Query._walkUpdatePath (c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\lib\query.js:2243:25) at Query._castUpdate (c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\lib\query.js:2122:23) at castDoc (c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\lib\query.js:2318:18) at Query._findAndModify (c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\lib\query.js:1669:17) at Query._findOneAndUpdate (c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\lib\query.js:1577:8) at c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\node_modules\kareem\index.js:156:8 at c:\var\www\beta.mayfieldafc.com\node_modules\mongoose\node_modules\kareem\index.js:18:7 at process._tickCallback (node.js:442:13)",
    message: "Cast to ObjectId failed for value "55b7f4825d3255b043e3dfe5,55b7f4825d3255b043e3dfe6,55b7f4825d3255b043e3dfe7,55b7f4825d3255b043e3dfe8" at path "leagues"",
    name: "CastError",
    kind: "ObjectId",
    value: [
    "55b7f4825d3255b043e3dfe5",
    "55b7f4825d3255b043e3dfe6",
    "55b7f4825d3255b043e3dfe7",
    "55b7f4825d3255b043e3dfe8",
    "55b7f4825d3255b043e3dfe9",
    "55c1fa1c2fc9527820e5f828"
    ],
    path: "leagues"
    }
}

我在这里不清楚,因为对我来说这是设置模型之间引用的确切方法。

您正在尝试添加一个包含 "id" 个值的数组。如果这是您想要的,那么您的模式应该是:

var Seasons = new Schema({
    leagues: [{ type: Schema.ObjectId, ref: 'Leagues', default: null }]
})

将 "leagues" 定义为一个数组,而目前它不是。