Goal
Fetching products by category id from mongoDB 'products' collection using mongoose.find
Excpected and actual results
All the documents having the matching category should return, in the limit number, but I receive an empty array instead. I tried to query using mongodb's compass - it worked as expected. I even tried my code to find documents using id(worked), title(worked) and price(didn't work). Can't find the beginning of the problem root.
Code
Product schema and model:
const mongoose = require('mongoose');
const productSchema = new mongoose.Schema({
title: {
type: String,
required: true,
},
thumbnail: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
category: {
type: mongoose.Types.ObjectId,
required: true,
},
rating: {
type: Number,
default: 0,
},
voters: {
type: Number,
default: 0,
},
});
.
.
.
const Product = mongoose.model('product', productSchema);
module.exports = { Product, ... };
Resolver:
products: (_, { limit, category }) => {
console.log(mongoose.Types.ObjectId('5ec120a9fc13ae248f000004')); // Works fine
Product.find({ category: mongoose.Types.ObjectId(category) }) // Tried also as a simple string, still not working
.limit(limit)
.exec((err, products) => {
if (err) console.log(err);
console.log(products);
});
if (category) return Product.find({ category }).limit(limit);
return Product.find({}).limit(limit);
},
Result recorded in the console:
5ec120a9fc13ae248f000004
[]
Collection 'products' sample:
{
"_id": {
"$oid": "5ec129e55db26438fcdb9b01"
},
"title": "Ecolab - Lime - A - Way 4/4 L",
"category": "5ec120a9fc13ae248f000004",
"thumbnail": "https://source.unsplash.com/350x390/?Automotive",
"price": "236.13",
"rating": 878210,
"voters": 2668388
},
{
"_id": {
"$oid": "5ec129e55db26438fcdb9b02"
},
"title": "Mushrooms - Black, Dried",
"category": "5ec120a9fc13ae248f000001",
"thumbnail": "https://source.unsplash.com/350x390/?Sports",
"price": "439.85",
"rating": 549879,
"voters": 2375685
},
{
"_id": {
"$oid": "5ec129e55db26438fcdb9b03"
},
"title": "Dried Figs",
"category": "5ec120a9fc13ae248f000004",
"thumbnail": "https://source.unsplash.com/350x390/?Automotive",
"price": "202.60",
"rating": 925701,
"voters": 2740499
}