How to search products,Category and brand by using Names
in mong.db , in nodejs,
am getting all items, but how sort and get the searching products,?
here is my search container,
user-header.hbs
<div class="search-container">
<form class="form-inline" action="/products/search/:key" method="get">
<input class=" search-input" type="search" name="searchTerm" class="form-control" placeholder="Search..."
aria-label="Search" />
</form>
</div>
product.js
here get router
router.get("/search/:key",(req, res)=> {
// var searchTerm = req.body.searchTerm
// console.log('regex',regex)
userHelpers.getAllSearchProducts(req.params.seraechresult
).then((product)=>{
console.log('result>>>>>>>',product)
res.render('user/search', {product});
})
})
user-helpers.js
through user helpers am geting the all prosucts but how to get the searching products ?
getAllSearchProducts:(seraechresult)=>{
return new Promise(async(resolve,reject)=>{
let SearchItems=await db.get().collection(collection.CART_COLLECTION)
.findOne(
{
"$or":[
{'Name':{'$regex':seraechresult}}
]
}
)
resolve(SearchItems)
})
},
here is my collection, products,
products [
{
_id: new ObjectId("62873c15c7becd09b1377fd8"),
Name: 'pixel 6',
Category: 'smart Phone',
Price: '60000',
Description: 'Note that to perform a smart search, DataTables uses regular expressions, so if enable regular expressions using the second parameter to this method, you will likely want to disable smart searching as the two regular expressions might otherwise conflict and cause unexpected results.',
Brand: 'Google'
},
{
_id: new ObjectId("628795e6a55d421aaca788cb"),
Name: 'iphone 11 pro ',
Category: 'smartphone',
Price: '100000',
Brand: 'apple',
Description: 'Super-strong materials and water resistance make iPhone incredibly durable. And automatic iOS updates deliver new features and security enhancements that keep your iPhone running beautifully and help it hold its value longer than other smartphones.'
},
{
_id: new ObjectId("6294f13458c9d3947bd2db5f"),
Name: 'samsung fold 2',
Category: 'smartphone',
Price: '1000000',
Brand: 'samsung',
Description: 'something going '
},
{
_id: new ObjectId("6295c129ff104b9c3d1eef9b"),
Name: 'lgg8x',
Category: 'smartphone',
Price: '280000',
Brand: 'LG',
Description: 'something'
}
]
I am not able to search the product by using mongodb how to solve this?