我正在尝试在mongo中创建一个文档但面临一些错误可能是由于mongodb架构不正确。请指正。提前谢谢。
MongoDB架构
const createMatchSchema = new Schema({
match_key : {
type : String,
required : true
},
match_status : {
type : String,
required : true
},
players : [{
fullname : String,
innings : [{
fielding : {
catches : Number,
runouts : Number,
stumbeds : Number
},
batting : {
dots : Number,
sixes : Number,
runs : Number,
balls : Number,
fours : Number,
strike_rate : Schema.Types.Decimal128,
dismissed : Boolean
},
bowling : {
dots: Number,
runs: Number,
balls: Number,
maiden_overs: Number,
wickets: Number,
extras: Number,
overs: String,
economy: Number
}
}],
identified_roles : {
keeper : { type : Boolean, default: false },
bowler : { type : Boolean, default: false },
batsman : { type : Boolean, default: false }
}
}],
createdOn : {
type : String,
default : moment().format('DD-MM-YYYY-hh-mm-ss')
}
});
module.exports = mongoose.model("MatchData", createMatchSchema, 'matchDataCollection');
JSON数据
{
"match_key": "match_key",
"match_status": "completed",
"players": [{
"fullname": "Dwayne Bravo",
"innings": [{
"fielding": {},
"bowling": {
"dots": 6,
"runs": 36,
"balls": 24,
"maiden_overs": 0,
"wickets": 2,
"extras": 2,
"overs": "4.0",
"economy": 9
},
"batting": {
"dots": 1,
"sixes": 1,
"runs": 15,
"balls": 9,
"fours": 0,
"strike_rate": 166.67,
"dismissed": false
}
}],
"identified_roles": {
"keeper": false,
"bowler": true,
"batsman": true
}
}]
}
查询
dd.findOneAndUpdate({'match_key':'aa'},JSONData,{upsert:true},function(err,resp){
if(err){
console.log(err);
res.json({'s':100});
}
else{
res.json(resp);
}
});
面对一些错误......
{ CastError: Cast to embedded failed for value "{ identified_roles: { batsman: true, bowler: true, keeper: false },\n innings: [ { batting: [Object], bowling: [Object], fielding: {} } ],\n fullname: \'Dwayne Bravo\' }" at path "players"
at new CastError (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\error\cast.js:27:11)
at DocumentArray.cast (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\schema\documentarray.js:340:19)
at castUpdateVal (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\services\query\castUpdate.js:313:22)
at walkUpdatePath (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\services\query\castUpdate.js:229:22)
at castUpdate (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\services\query\castUpdate.js:72:18)
at model.Query._castUpdate (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:3181:10)
at castDoc (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:3207:18)
at model.Query.Query._findAndModify (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:2327:19)
at model.Query.Query._findOneAndUpdate (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:2162:8)
at E:\SportsWin\new-nodeserver\node_modules\kareem\index.js:250:8
at E:\SportsWin\new-nodeserver\node_modules\kareem\index.js:23:7
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
message: 'Cast to embedded failed for value "{ identified_roles: { batsman: true, bowler: true, keeper: false },\\n innings: [ { batting: [Object], bowling: [Object], fielding: {} } ],\\n fullname: \\\'Dwayne Bravo\\\' }" at path "players"',
name: 'CastError',
stringValue: '"{ identified_roles: { batsman: true, bowler: true, keeper: false },\\n innings: [ { batting: [Object], bowling: [Object], fielding: {} } ],\\n fullname: \\\'Dwayne Bravo\\\' }"',
kind: 'embedded',
value: '{ identified_roles: { batsman: true, bowler: true, keeper: false },\n innings: [ { batting: [Object], bowling: [Object], fielding: {} } ],\n fullname: \'Dwayne Bravo\' }',
path: 'players',
reason:
{ CastError: Cast to Array failed for value "[ { batting:
{ dismissed: false,
strike_rate: 166.67,
fours: 0,
balls: 9,
runs: 15,
sixes: 1,
dots: 1 },
bowling:
{ economy: 9,
overs: '4.0',
extras: 2,
wickets: 2,
maiden_overs: 0,
balls: 24,
runs: 36,
dots: 6 },
fielding: {} } ]" at path "innings"
at new CastError (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\error\cast.js:27:11)
at EmbeddedDocument.$set (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\document.js:773:7)
at EmbeddedDocument._handleIndex (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\document.js:594:14)
at EmbeddedDocument.$set (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\document.js:554:24)
at EmbeddedDocument.Document (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\document.js:83:12)
at EmbeddedDocument [as constructor] (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\types\embedded.js:31:12)
at new EmbeddedDocument (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\schema\documentarray.js:74:17)
at DocumentArray.cast (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\schema\documentarray.js:333:22)
at castUpdateVal (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\services\query\castUpdate.js:313:22)
at walkUpdatePath (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\services\query\castUpdate.js:229:22)
at castUpdate (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\services\query\castUpdate.js:72:18)
at model.Query._castUpdate (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:3181:10)
at castDoc (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:3207:18)
at model.Query.Query._findAndModify (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:2327:19)
at model.Query.Query._findOneAndUpdate (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:2162:8)
at E:\SportsWin\new-nodeserver\node_modules\kareem\index.js:250:8
at E:\SportsWin\new-nodeserver\node_modules\kareem\index.js:23:7
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
message: 'Cast to Array failed for value "[ { batting: \n { dismissed: false,\n strike_rate: 166.67,\n fours: 0,\n balls: 9,\n runs: 15,\n sixes: 1,\n dots: 1 },\n bowling: \n { economy: 9,\n overs: \'4.0\',\n extras: 2,\n wickets: 2,\n maiden_overs: 0,\n balls: 24,\n runs: 36,\n dots: 6 },\n fielding: {} } ]" at path "innings"',
name: 'CastError',
stringValue: '"[ { batting: \n { dismissed: false,\n strike_rate: 166.67,\n fours: 0,\n balls: 9,\n runs: 15,\n sixes: 1,\n dots: 1 },\n bowling: \n { economy: 9,\n overs: \'4.0\',\n extras: 2,\n wickets: 2,\n maiden_overs: 0,\n balls: 24,\n runs: 36,\n dots: 6 },\n fielding: {} } ]"',
kind: 'Array',
value: [ [Object] ],
path: 'innings',
reason:
{ CastError: Cast to embedded failed for value "{ batting: \n { dismissed: false,\n strike_rate: 166.67,\n fours: 0,\n balls: 9,\n runs: 15,\n sixes: 1,\n dots: 1 },\n bowling: \n { economy: 9,\n overs: \'4.0\',\n extras: 2,\n wickets: 2,\n maiden_overs: 0,\n balls: 24,\n runs: 36,\n dots: 6 },\n fielding: {} }" at path "innings"
at new CastError (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\error\cast.js:27:11)
at DocumentArray.cast (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\schema\documentarray.js:340:19)
at DocumentArray.SchemaType.applySetters (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\schematype.js:705:12)
at EmbeddedDocument.$set (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\document.js:764:18)
at EmbeddedDocument._handleIndex (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\document.js:594:14)
at EmbeddedDocument.$set (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\document.js:554:24)
at EmbeddedDocument.Document (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\document.js:83:12)
at EmbeddedDocument [as constructor] (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\types\embedded.js:31:12)
at new EmbeddedDocument (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\schema\documentarray.js:74:17)
at DocumentArray.cast (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\schema\documentarray.js:333:22)
at castUpdateVal (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\services\query\castUpdate.js:313:22)
at walkUpdatePath (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\services\query\castUpdate.js:229:22)
at castUpdate (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\services\query\castUpdate.js:72:18)
at model.Query._castUpdate (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:3181:10)
at castDoc (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:3207:18)
at model.Query.Query._findAndModify (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:2327:19)
at model.Query.Query._findOneAndUpdate (E:\SportsWin\new-nodeserver\node_modules\mongoose\lib\query.js:2162:8)
at E:\SportsWin\new-nodeserver\node_modules\kareem\index.js:250:8
at E:\SportsWin\new-nodeserver\node_modules\kareem\index.js:23:7
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)
message: 'Cast to embedded failed for value "{ batting: \\n { dismissed: false,\\n strike_rate: 166.67,\\n fours: 0,\\n balls: 9,\\n runs: 15,\\n sixes: 1,\\n dots: 1 },\\n bowling: \\n { economy: 9,\\n overs: \\\'4.0\\\',\\n extras: 2,\\n wickets: 2,\\n maiden_overs: 0,\\n balls: 24,\\n runs: 36,\\n dots: 6 },\\n fielding: {} }" at path "innings"',
name: 'CastError',
stringValue: '"{ batting: \\n { dismissed: false,\\n strike_rate: 166.67,\\n fours: 0,\\n balls: 9,\\n runs: 15,\\n sixes: 1,\\n dots: 1 },\\n bowling: \\n { economy: 9,\\n overs: \\\'4.0\\\',\\n extras: 2,\\n wickets: 2,\\n maiden_overs: 0,\\n balls: 24,\\n runs: 36,\\n dots: 6 },\\n fielding: {} }"',
kind: 'embedded',
value: '{ batting: \n { dismissed: false,\n strike_rate: 166.67,\n fours: 0,\n balls: 9,\n runs: 15,\n sixes: 1,\n dots: 1 },\n bowling: \n { economy: 9,\n overs: \'4.0\',\n extras: 2,\n wickets: 2,\n maiden_overs: 0,\n balls: 24,\n runs: 36,\n dots: 6 },\n fielding: {} }',
path: 'innings',
reason: [MongooseError] } } }
请帮助我....