[英]Finding documents that fit json input in mongodb
我正在嘗試執行一個寫入語句,以查找所有適合通過表單提供的 JSON 數據的文檔。 這是 JSON 的外觀示例。 我在具有這些完全相同信息的數據庫上創建了一個文檔,但我仍然沒有得到任何回報。
{
type: 'Diamond',
weight: '1',
size: '1',
color: 'Red',
origin: 'Mexico',
clarity: 'Translucent',
shape: 'Pear'
}
這是我的代碼:
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 })
}
for 循環后的 JSON 如下:
{
type: 'Diamond',
weight: 1,
size: 1,
color: 'Red',
origin: 'Mexico',
clarity: 'Translucent',
shape: 'Pear'
}
首先,您應該使用 mongoose Schema 創建一個模型。 'Model.findOne()' 方法應該可以解決您的問題。
方法文檔: https ://mongoosejs.com/docs/api.html#model_Model.findOne
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.