将对象推入两级数组

时间:2017-07-13 00:02:18

标签: node.js mongodb mongoose mongodb-query

我在stackoverflow上看到了类似的问题,但没有一个对我有用 我还阅读了有关更新的mongodb文档,但我所寻找的并不存在 我的模型Schema

subSchoolSchema名为subschool
var subSchoolSchema = mongoose.Schema({
    schoolid: String,
    username: String,
    password: String,
    departments: [{
        name: String,
        hod: String,
        duration: Number,
        classes: [{
            title: String,
            students: [{
                schId: String,
                firstname: String,
                lastname: String,
                othername: String,
                password: String,
                email: String,
                group: String,
                issues: Array,
                notes: String,
                chatId: String,
                fcm_token: String,
                state: Number,
                type: Number,
                suspended: Boolean,
                details: [{
                    key: String,
                    value: String
                }],
                courses: [{
                    title: String,
                    code: String,
                    credit: Number,
                    instructor: String,
                    instructorId: String,
                    score: Number
                }],
                img: {
                    type: String,
                    default: "svg/icon_student_sync_dp.svg"
                }
            }]
        }]
    }]
})

我想使用此代码将对象推送到departments.classes.students

var stud = { schId: id, firstname: fname, lastname: lname, othername: othername, email: email, group: grp, password: password, state: 2, type: 0, img: dp, details: details }
subschool.update({ username: request.session.code, "departments.classes._id": studentClass }, {
    "$push": {
        "departments.classes.students" : stud
            }
    }, function (er, result) {
        if(er){
            console.log(er)
        } else {
            res.end('{"status" : 0,"success":1}')
        }
}

我一直收到此错误

{ CastError: Cast to [number] failed for value "[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password"
:"7f6f5e9d","state":2,"type":0,"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]" at path "student
s"
    at MongooseError.CastError (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\error\cast.js:26:11)
    at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:184:15)
    at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:195:15)
    at castUpdateVal (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:266:22)
    at walkUpdatePath (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:143:22)
    at castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:71:18)
    at Query._castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2902:10)
    at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2752:23)
    at Query.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2547:10)
    at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2448:16)
    at Function.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2364:10)
    at C:\Users\USER\Documents\Projects\School Sync\ikey\routes\students\studentapi.js:33:15
    at Query.<anonymous> (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:3800:16)
    at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:277:21
    at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:131:16
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
  message: 'Cast to [number] failed for value "[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":
"7f6f5e9d","state":2,"type":0,"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]" at path "students
"',
  name: 'CastError',
  stringValue: '"[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":"7f6f5e9d","state":2,"type":0,
"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]"',
  kind: '[number]',
  value: '[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":"7f6f5e9d","state":2,"type":0,"img":"
svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]',
  path: 'students',
  reason:
   { CastError: Cast to number failed for value "{ schId: 'nj122',
     firstname: 'djnk',
     lastname: 'jjkn',
     othername: 'kn',
     email: 'inei@iiu.edn',
     group: 'B',
     password: '7f6f5e9d',
     state: 2,
     type: 0,
     img: 'svg/icon_student_sync_dp.svg',
     details:
      [ { key: 'Gender', value: 'Male' },
        { key: 'Blood Type', value: 'V0' } ] }" at path "students"
       at MongooseError.CastError (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\error\cast.js:26:11)
       at SchemaNumber.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\number.js:231:9)
       at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:180:34)
       at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:195:15)
       at castUpdateVal (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:266:22)
       at walkUpdatePath (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:143:22)
       at castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:71:18)
       at Query._castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2902:10)
       at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2752:23)
       at Query.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2547:10)
       at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2448:16)
       at Function.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2364:10)
       at C:\Users\USER\Documents\Projects\School Sync\ikey\routes\students\studentapi.js:33:15
       at Query.<anonymous> (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:3800:16)
       at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:277:21
       at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:131:16
     message: 'Cast to number failed for value "{ schId: \'nj122\',\n  firstname: \'djnk\',\n  lastname: \'jjkn\',\n  othername: \'kn\',\n  email: \'inei@iiu.edn\',\
n  group: \'B\',\n  password: \'7f6f5e9d\',\n  state: 2,\n  type: 0,\n  img: \'svg/icon_student_sync_dp.svg\',\n  details: \n   [ { key: \'Gender\', value: \'Male\'
},\n     { key: \'Blood Type\', value: \'V0\' } ] }" at path "students"',
     name: 'CastError',
     stringValue: '"{ schId: \'nj122\',\n  firstname: \'djnk\',\n  lastname: \'jjkn\',\n  othername: \'kn\',\n  email: \'inei@iiu.edn\',\n  group: \'B\',\n  password
: \'7f6f5e9d\',\n  state: 2,\n  type: 0,\n  img: \'svg/icon_student_sync_dp.svg\',\n  details: \n   [ { key: \'Gender\', value: \'Male\' },\n     { key: \'Blood Type
\', value: \'V0\' } ] }"',
     kind: 'number',
     value:
      { schId: 'nj122',
        firstname: 'djnk',
        lastname: 'jjkn',
        othername: 'kn',
        email: 'inei@iiu.edn',
        group: 'B',
        password: '7f6f5e9d',
        state: 2,
        type: 0,
        img: 'svg/icon_student_sync_dp.svg',
        details: [Object] },
     path: 'students',
     reason: undefined } }
{ username: 'AA',
  'departments.classes._id': '59669687abf46f170045cc6e' }
{ CastError: Cast to [number] failed for value "[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password"
:"7f6f5e9d","state":2,"type":0,"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]" at path "student
s"
    at MongooseError.CastError (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\error\cast.js:26:11)
    at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:184:15)
    at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:195:15)
    at castUpdateVal (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:266:22)
    at walkUpdatePath (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:143:22)
    at castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:71:18)
    at Query._castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2902:10)
    at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2752:23)
    at Query.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2547:10)
    at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2448:16)
    at Function.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2364:10)
    at C:\Users\USER\Documents\Projects\School Sync\ikey\routes\students\studentapi.js:33:15
    at Query.<anonymous> (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:3800:16)
    at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:277:21
    at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:131:16
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
  message: 'Cast to [number] failed for value "[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":
"7f6f5e9d","state":2,"type":0,"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]" at path "students
"',
  name: 'CastError',
  stringValue: '"[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":"7f6f5e9d","state":2,"type":0,
"img":"svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]"',
  kind: '[number]',
  value: '[{"schId":"nj122","firstname":"djnk","lastname":"jjkn","othername":"kn","email":"inei@iiu.edn","group":"B","password":"7f6f5e9d","state":2,"type":0,"img":"
svg/icon_student_sync_dp.svg","details":[{"key":"Gender","value":"Male"},{"key":"Blood Type","value":"V0"}]}]',
  path: 'students',
  reason:
   { CastError: Cast to number failed for value "{ schId: 'nj122',
     firstname: 'djnk',
     lastname: 'jjkn',
     othername: 'kn',
     email: 'inei@iiu.edn',
     group: 'B',
     password: '7f6f5e9d',
     state: 2,
     type: 0,
     img: 'svg/icon_student_sync_dp.svg',
     details:
      [ { key: 'Gender', value: 'Male' },
        { key: 'Blood Type', value: 'V0' } ] }" at path "students"
       at MongooseError.CastError (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\error\cast.js:26:11)
       at SchemaNumber.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\number.js:231:9)
       at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:180:34)
       at SchemaArray.cast (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\schema\array.js:195:15)
       at castUpdateVal (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:266:22)
       at walkUpdatePath (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:143:22)
       at castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\services\query\castUpdate.js:71:18)
       at Query._castUpdate (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2902:10)
       at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2752:23)
       at Query.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\query.js:2547:10)
       at _update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2448:16)
       at Function.update (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:2364:10)
       at C:\Users\USER\Documents\Projects\School Sync\ikey\routes\students\studentapi.js:33:15
       at Query.<anonymous> (C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\mongoose\lib\model.js:3800:16)
       at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:277:21
       at C:\Users\USER\Documents\Projects\School Sync\ikey\node_modules\kareem\index.js:131:16
     message: 'Cast to number failed for value "{ schId: \'nj122\',\n  firstname: \'djnk\',\n  lastname: \'jjkn\',\n  othername: \'kn\',\n  email: \'inei@iiu.edn\',\
n  group: \'B\',\n  password: \'7f6f5e9d\',\n  state: 2,\n  type: 0,\n  img: \'svg/icon_student_sync_dp.svg\',\n  details: \n   [ { key: \'Gender\', value: \'Male\'
},\n     { key: \'Blood Type\', value: \'V0\' } ] }" at path "students"',
     name: 'CastError',
     stringValue: '"{ schId: \'nj122\',\n  firstname: \'djnk\',\n  lastname: \'jjkn\',\n  othername: \'kn\',\n  email: \'inei@iiu.edn\',\n  group: \'B\',\n  password
: \'7f6f5e9d\',\n  state: 2,\n  type: 0,\n  img: \'svg/icon_student_sync_dp.svg\',\n  details: \n   [ { key: \'Gender\', value: \'Male\' },\n     { key: \'Blood Type
\', value: \'V0\' } ] }"',
     kind: 'number',
     value:
      { schId: 'nj122',
        firstname: 'djnk',
        lastname: 'jjkn',
        othername: 'kn',
        email: 'inei@iiu.edn',
        group: 'B',
        password: '7f6f5e9d',
        state: 2,
        type: 0,
        img: 'svg/icon_student_sync_dp.svg',
        details: [Object] },
     path: 'students',
     reason: undefined } }

我不知道如何避免此错误,它会一直显示强制转换错误,但在上面的架构中我将其定义为array。如果我能得到这个问题的解决方案,我会很高兴。感谢。

0 个答案:

没有答案