2

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.

SebastianG
  • 8,563
  • 8
  • 47
  • 111
  • 1
    [.lean()](https://mongoosejs.com/docs/tutorials/lean.html) does exactly that. – Tomasz Kasperczyk Jul 25 '19 at 14:20
  • @TomaszKasperczyk I tried lean and it only returns the _id – SebastianG Jul 25 '19 at 15:16
  • Can you update your question to include your code that sends the response? – JohnnyHK Jul 25 '19 at 18:51
  • @JohnnyHK it's a firebase function so it is literally return result -- nothing more. Might be a firebase functions issue? In the end I made a for loop and called .toObject() on each document of the array, which sucks performance wise imo. I have added the loop code. – SebastianG Jul 26 '19 at 13:54
  • 1
    @TomaszKasperczyk I ended up calling it before .exec() -- i used the examples on other stackoverflow threads and added lean as a parameter after the search query which returned only the ID, since yours is the good answer and you were first, if you post it as an answer I will mark it as the solution. – SebastianG Jul 26 '19 at 13:59
  • @MichaelB it turned out to be a duplicate question, can't answer anymore. I'm glad you've solved it. – Tomasz Kasperczyk Jul 26 '19 at 15:35
  • @TomaszKasperczyk It's not a duplicate, has nothing to do with spread syntax, It already has 3 reopen votes, I'm sure people will reopen it soon. – SebastianG Jul 26 '19 at 15:48
  • You are getting decorated mongoose document. The duplicate answer suggest the same to use either `.lean` or `.toObject`. – Ashh Jul 27 '19 at 20:01

1 Answers1

1

Mongoose always return an instance mongoose object, which is immutable by default. To get the plain JS object, try query with lean() like this:

const query = Job.find({
    _id: {
        $in: data.jobs
    }
});

let result = await query.lean().exec();

You can read more on lean at: https://mongoosejs.com/docs/tutorials/lean.html

Hope this helps :)

Mohammed Amir Ansari
  • 2,311
  • 2
  • 12
  • 26