My array of docs looks like this once retreived from the backend:
41:
$__: {strictMode: true, selected: {…}, shardval: null, saveError: null, validationError: {…}, …}
$init: true
$locals: {}
errors: {undefined: {…}, files: {…}}
isNew: false
_doc: {status: "Finished", isOnHold: false, requirements: Array(0), files: Array(0), reportFileIds: Array(1), …}
__proto__: Object
The actual doc being under _doc for each item. This is the result of a mongoose.Find query:
let query = Job.find({
_id: {
$in: data.jobs
}
});
let result = await query.exec();
Now if I try to get items of that result array before I send it, it doesn't look like what's actually modelled on the frontend which is weird.
Is there any way to get just the pure array of documents using mongoose.find? I cannot use aggregate queries as I don't want to manually project each possible property.
EDIT:
This is not a duplicate for crying out loud, spread syntax has nothing to do with it, do you see me mentioning the spread syntax anywhere? This is related to the mongoose driver.
Edit2: I ended up looping over the array and calling .toObject on each item:
let jobs = await query.exec();
let result = [];
for (let doc of jobs) {
result.push(doc.toObject());
}
return result;
Edit3: Ended up calling query.lean().exec()
which did the right thing. I used the lean part as a parameter in the past based on a stackoverflow example that's probably outdated and it would only return the ID's of the documents. But calling it like above works fine.