question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Querying populated fields return empty array

See original GitHub issue

Describe the bug When you query a subdocument that has been populated, the query returns empty array.

To Reproduce Steps to reproduce the behavior: 1.- Create a Schema with populated fields.

const ProjectSchema = new mongoose.Schema(
    {
        name: {
            en: {
                type: String,
                required: true,
            },
            es: {
                type: String,
                required: true,
            },
        },
        maker: { type: ObjectId, ref: 'Maker' }
}

2.- Create an endpoint to retrieve items from the new model using paginate with a filter using a populated field:

const getItemsFromDB = async (req, query) => {
const options = {
        sort: 'createdAt',
        lean: true,
        1,
        5,
         { path: 'maker', select: 'name' },
    }
    return new Promise((resolve, reject) => {
        model.paginate(query, options, (err, items) => {
            if (err) {
                reject(buildErrObject(422, err.message))
            }
            resolve(cleanPaginationID(items))
        })
    })
}

3.- Call the endpoint querying the populated field:

{{server}}/projects?filter={"maker.name":"Ocean Sentry"}&page=1&limit=2&sort=name&order=-1

4.- Get an empty array in the results:

{
    "docs": [],
    "totalDocs": 0,
    "limit": 2,
    "hasPrevPage": false,
    "hasNextPage": false,
    "page": 1,
    "totalPages": 1,
    "pagingCounter": 1,
    "prevPage": null,
    "nextPage": null
}

Expected behavior An array containing the results. If you query for a field with subdocuments (not populated) it works correctly.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
aravindnccommented, Mar 5, 2019

@ramoncardena Great. Cool. You have to edit the write-up there is a typo,

for more complex cases mongoose-aggregate-paginate-v2 is the right choice.
0reactions
otaviobpscommented, Aug 5, 2019

Hi @ramoncardena how did you do your query? I’m getting an empty array when I try to query a field of the populated collection:

const myAggregate = Order.aggregate([
      {
        $lookup: {
          from: "users",
          localField: "user",
          foreignField: "_id",
          as: "user"
        }
      },
      {
        $unwind: "$user"
      },
      {
        $match: { user: { firstName: "Francisco" } }
      }
    ]);

If I skip the query, I get the fields populated, so I’m sure is something with the query. I’m using the mongoose-aggregate-paginate-v2, however I can’t query from a populated field.

Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Querying populated fields return empty array #21 - GitHub
When you query a subdocument that has been populated, the query returns empty array. ... Steps to reproduce the behavior: 1.- Create a...
Read more >
Mongoose populate() returning empty array - Stack Overflow
I'm trying to do a simple populate() to my model. I have a User model and Store model. The User has a favoriteStores...
Read more >
[Solved]-Mongoose populate returns empty array but returns ...
Coding example for the question Mongoose populate returns empty array but returns data without populate-mongodb.
Read more >
Project Fields to Return from Query — MongoDB Manual
For fields that contain arrays, MongoDB provides the following projection operators for manipulating arrays: $elemMatch , $slice , and $ . ... $elemMatch...
Read more >
What is find() in Mongoose? - Educative.io
When a document is queried using a certain field, it either returns 1 , more than one, or an empty array of documents...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found