'Finding documents that fit json input in mongodb
I'm trying to do a write statement that finds all the documents that fit the JSON data fed through a form. Here's an example of what the JSON looks like. I created a document on the database that has these exact same info and I still get nothing in return.
{
type: 'Diamond',
weight: '1',
size: '1',
color: 'Red',
origin: 'Mexico',
clarity: 'Translucent',
shape: 'Pear'
}
and here's my code:
module.exports.renderSpecifics = async (req, res) => {
const query = await req.query
for (let key in query) {
console.log(typeof key)
if (Number(query[key])) {
query[key] = parseInt(query[key])
}
}
console.log(query)
const campgrounds = await Campground.findMany({ query })
console.log(campgrounds)
res.render('campgrounds/index', { campgrounds })
}
the JSON after the for loop is the folllowing:
{
type: 'Diamond',
weight: 1,
size: 1,
color: 'Red',
origin: 'Mexico',
clarity: 'Translucent',
shape: 'Pear'
}
Solution 1:[1]
first you should create a model with mongoose Schema. The 'Model.findOne()' method should solve your problem.
Method Docs: https://mongoosejs.com/docs/api.html#model_Model.findOne
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ?smail Can Karata? |