Querying populated fields return empty array
See original GitHub issueDescribe 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:
- Created 5 years ago
- Comments:9 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@ramoncardena Great. Cool. You have to edit the write-up there is a typo,
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:
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!