node.js - Mongoose one to many relationship -


i have mongoose schema user data:

// user schema const user = new schema( {    name: {type: string},    email: {type: string, unique: true},    // other fields }) 

and user's daily statistics schema:

// stats schema const stats = new schema( {   datecreated: {type: date, default: date.now()},   stepswalked: {type: number, default: 0},   // other fields   userid: string  // user id field }) 

when trying generate multiple stats schema objects same user id this:

for (let = 0; < 40; ++i) {   statsdata = await stats.create({     userid: userdata._id   }) } 

i'm getting mongoose duplicate exception on second iteration of loop. stack trace:

mongoerror: e11000 duplicate key error collection: 5909aed3df9db12e2b71a579_.stats index: userid_1 dup key: { : "5991c027a572690bfd322c08" } @ function.mongoerror.create (node_modules/mongodb-core/lib/error.js:31:11) @ toerror (node_modules/mongodb/lib/utils.js:139:22) @ node_modules/mongodb/lib/collection.js:669:23 @ handlecallback (node_modules/mongodb/lib/utils.js:120:56) @ node_modules/mongodb/lib/bulk/unordered.js:465:9 @ handlecallback (node_modules/mongodb/lib/utils.js:120:56) @ resulthandler (node_modules/mongodb/lib/bulk/unordered.js:413:5) @ node_modules/mongodb-core/lib/connection/pool.js:469:18 @ _combinedtickcallback (internal/process/next_tick.js:131:7) @ process._tickcallback (internal/process/next_tick.js:180:9) 

how can implement one-to-many relationship mongoose ? have huge amount of stats data single user, can't store stats data part of user schema this:

// user schema const user = new schema( {    name: {type: string, default: 'nan'},    email: {type: string, unique: true, default: 'nan'},    // other fields    stats: [stats]   // many docs store array in schema }) 


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -