How to execute a search query in Mongoose?

I have a collection of ebooks data in mongodb like

{ "_id" : ObjectId("58b56fe19585b10cd42981d8"), "cover_path" : "D:\\Ebooks\\uploads\\ebooks\\cover\\1488285665748-img1-700x400.jpg", "path" : "D:\\Ebooks\\uploads\\ebooks\\pdf\\1488285665257-Webservices Natraz.pdf", "description" : "ebook", "title" : "book name", "tag" : [ "Hindi", "Other" ], "__v" : NumberInt(0) } 

Now I want to find something if the keyword matches a little from "title:" , and then show all related book objects.

My Mongoose scheme: -

 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var EbookSchema = new Schema({ title: {type:String}, description: {type:String}, path: {type:String,required:true}, cover_path: {type:String,required:true}, tag: [{ type: String }] }); module.exports = mongoose.model('Ebook', EbookSchema); 

I'm trying to: -

 app.get('/ebook?search=',function(req,res){ var search_key = req.param('search'); Ebook.find(title:'search',function(err, ebooks) { if (err) res.send(err); res.json(ebooks); }); }); 

but i found zero, how can i do this? I only want, when I search for a keyword with bits, I found all related objects.

+7
source share
1 answer

Try wrapping your request in curly, Mongoose expects an object as a request.

 app.get('/ebook?search=',function(req,res){ var search_key = req.param('search'); Ebook.find({title: search_key}) .then(ebooks => res.json(ebooks)) .catch(err => res.status(404).json({ success: false })); }); 
+4
source

Source: https://habr.com/ru/post/1264905/


All Articles