简体   繁体   中英

Mongoose forEach is not a function

I have a database of 50,000 users and I need to loop over each one and update them with some dynamic values per user.

I've tried using

users.find({}).forEach((user) => { console.log('Some code.') })

But it says that forEach is not a function - from research it looks like this is a mongodb function but not a mongoose function.

I've tried using just find({}) but of course the results will be cursor based, and it also doesn't actually console.log anything for me.

I've also tried using .stream() , but that's also returning not a function and I've also tried using .batchSize(100) but that also does not return anything in the console log when testing.

I've looked into updateMany but that is only good for updating the same static value in a field.

I'm a little lost on how to proceed forwards in a performant way. Any ideas?

Thanks,

The Model.find() function returns a promise, not an array.

You will need to use await or a callback to actually execute the find and retrieve the result array before iterating.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM