我正在尝试在一个集合中保存一个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"
}
}
我在这里不清楚,对我而言,这是在模型之间设置引用的确切方法。
答案 0 :(得分:1)
您正在尝试添加一个" id"值。如果这是你想要的,那么你的架构应该是:
var Seasons = new Schema({
leagues: [{ type: Schema.ObjectId, ref: 'Leagues', default: null }]
})
哪个定义"联盟"现在它不是一个阵列。