Currently I’m developing an APP using Node.JS and MongoDB. I’m trying to insert multiple documents with predefined _id s and some ObjectId arrays.
When I use insertMany all documents _id fields are strings instead of ObjectId. However when I use create and pass an array it works fine but instead of one query it performs separate query for each item of the array.
As it is obvious as the array grows it would be critical problem.
How can I use insertMany with the fields of type ObjectId instead of string?
2
Answers
I realized that by my default toObject settings the problem is resolved. I just need to repeat that settings where ever I want to convert the doc to object.
You need to wrap the _id string with the ObjectId type so it’s not treated as a string.