Automattic / mongoose

MongoDB object modeling designed to work in an asynchronous environment.
https://mongoosejs.com
MIT License
26.94k stars 3.84k forks source link

Cannot create property '$set' on number 0 #7358

Closed bonesoul closed 5 years ago

bonesoul commented 5 years ago

bug report, running mongoose 5.4.0.

What is the current behavior?

trying to run this bulkWrite operation;

  [  { filter: { game: 5b2b9d11c1914375252d03c6, year: 2018 },
       update:
        { '$set':
           { 'buffer.months.12.epoch': 1543611600,
             'buffer.months.12.days.21.epoch': 1545339600,
             'buffer.months.12.days.21.hours.18.epoch': 1545404400,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.viewers': 134130,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.channels': 2365 } },
       upsert: true } },
  { updateOne:
     { filter: { game: 5b2b9d11c1914375252d03c8, year: 2018 },
       update:
        { '$set':
           { 'buffer.months.12.epoch': 1543611600,
             'buffer.months.12.days.21.epoch': 1545339600,
             'buffer.months.12.days.21.hours.18.epoch': 1545404400,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.viewers': 124688,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.channels': 10462 } },
       upsert: true } },
  { updateOne:
     { filter: { game: 5c1cc98d7cadbe2b902b267e, year: 2018 },
       update:
        { '$set':
           { 'buffer.months.12.epoch': 1543611600,
             'buffer.months.12.days.21.epoch': 1545339600,
             'buffer.months.12.days.21.hours.18.epoch': 1545404400,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.viewers': 102508,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.channels': 1038 } },
       upsert: true } },
  { updateOne:
     { filter: { game: 5b2b9d11c1914375252d03c9, year: 2018 },
       update:
        { '$set':
           { 'buffer.months.12.epoch': 1543611600,
             'buffer.months.12.days.21.epoch': 1545339600,
             'buffer.months.12.days.21.hours.18.epoch': 1545404400,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.viewers': 52754,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.channels': 269 } },
       upsert: true } },
  { updateOne:
     { filter: { game: 5b2b9d11c1914375252d03c7, year: 2018 },
       update:
        { '$set':
           { 'buffer.months.12.epoch': 1543611600,
             'buffer.months.12.days.21.epoch': 1545339600,
             'buffer.months.12.days.21.hours.18.epoch': 1545404400,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.viewers': 50048,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.channels': 698 } },
       upsert: true } },
  { updateOne:
     { filter: { game: 5b2b9d11c1914375252d03ca, year: 2018 },
       update:
        { '$set':
           { 'buffer.months.12.epoch': 1543611600,
             'buffer.months.12.days.21.epoch': 1545339600,
             'buffer.months.12.days.21.hours.18.epoch': 1545404400,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.viewers': 45053,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.channels': 1770 } },
       upsert: true } },
  { updateOne:
     { filter: { game: 5b2b9d11c1914375252d03cb, year: 2018 },
       update:
        { '$set':
           { 'buffer.months.12.epoch': 1543611600,
             'buffer.months.12.days.21.epoch': 1545339600,
             'buffer.months.12.days.21.hours.18.epoch': 1545404400,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.viewers': 25395,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.channels': 1226 } },
       upsert: true } },
  { updateOne:
     { filter: { game: 5b2b9d11c1914375252d03d3, year: 2018 },
       update:
        { '$set':
           { 'buffer.months.12.epoch': 1543611600,
             'buffer.months.12.days.21.epoch': 1545339600,
             'buffer.months.12.days.21.hours.18.epoch': 1545404400,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.viewers': 24906,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.channels': 467 } },
       upsert: true } },
  { updateOne:
     { filter: { game: 5b2b9d11c1914375252d03cf, year: 2018 },
       update:
        { '$set':
           { 'buffer.months.12.epoch': 1543611600,
             'buffer.months.12.days.21.epoch': 1545339600,
             'buffer.months.12.days.21.hours.18.epoch': 1545404400,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.viewers': 21723,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.channels': 1067 } },
       upsert: true } },
  { updateOne:
     { filter: { game: 5b2bafe8c1914375252d0ba6, year: 2018 },
       update:
        { '$set':
           { 'buffer.months.12.epoch': 1543611600,
             'buffer.months.12.days.21.epoch': 1545339600,
             'buffer.months.12.days.21.hours.18.epoch': 1545404400,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.viewers': 20865,
             'buffer.months.12.days.21.hours.18.minutes.20.twitch.channels': 636 } },
       upsert: true } } ]

running it with;

await Game.bulkWrite(operations); // execute the bulk update operations.

getting the error;

{ message: 'Cannot create property \'$set\' on number \'0\'',
  stack:
   'TypeError: Cannot create property \'$set\' on number \'0\'\n    at applyTimestampsToUpdate (E:\\Coding\\hypetrain\\node_modules\\mongoose\\lib\\helpers\\update\\applyTimestampsToUpdate.js:40:16)\n    at E:\\Coding\\hypetrain\\node_modules\\mongoose\\lib\\helpers\\model\\castBulkWrite.js:50:11\n    at E:\\Coding\\hypetrain\\node_modules\\mongoose\\node_modules\\async\\internal\\parallel.js:31:39\n    at eachOfArrayLike (E:\\Coding\\hypetrain\\node_modules\\mongoose\\node_modules\\async\\eachOf.js:65:9)\n    at exports.default (E:\\Coding\\hypetrain\\node_modules\\mongoose\\node_modules\\async\\eachOf.js:9:5)\n    at _parallel (E:\\Coding\\hypetrain\\node_modules\\mongoose\\node_modules\\async\\internal\\parallel.js:30:5)\n    at parallelLimit (E:\\Coding\\hypetrain\\node_modules\\mongoose\\node_modules\\async\\parallel.js:88:26)\n    at utils.promiseOrCallback.cb (E:\\Coding\\hypetrain\\node_modules\\mongoose\\lib\\model.js:3037:5)\n    at Promise (E:\\Coding\\hypetrain\\node_modules\\mongoose\\lib\\utils.js:262:5)\n    at Promise._execute (E:\\Coding\\hypetrain\\node_modules\\bluebird\\js\\release\\debuggability.js:313:9)\n    at Promise._resolveFromExecutor (E:\\Coding\\hypetrain\\node_modules\\bluebird\\js\\release\\promise.js:483:18)\n    at new Promise (E:\\Coding\\hypetrain\\node_modules\\bluebird\\js\\release\\promise.js:79:10)\n    at Object.promiseOrCallback (E:\\Coding\\hypetrain\\node_modules\\mongoose\\lib\\utils.js:261:10)\n    at Function.Model.bulkWrite (E:\\Coding\\hypetrain\\node_modules\\mongoose\\lib\\model.js:3036:16)\n    at commitHistoricData (E:\\Coding\\hypetrain\\src\\tasks\\definitions\\services\\twitch\\update.js:149:16)' }

pointing here; https://github.com/Automattic/mongoose/blob/3cc4985bdb10e4ddcc9ed9947c027528030c33e5/lib/helpers/update/applyTimestampsToUpdate.js#L40

any ideas?

bonesoul commented 5 years ago

tracked it down to here;

https://github.com/Automattic/mongoose/blob/0ef6d1627b45a68c6cf58b978b921b27e0de9e0a/lib/helpers/model/castBulkWrite.js#L37

somehow op['update'] is set to 0 here.

bonesoul commented 5 years ago

found the fix, was my bad.

vkarpov15 commented 5 years ago

@bonesoul can you share what the fix was?

bonesoul commented 5 years ago

using the correct model instance :)