2017-03-01 15 views
5

i Şimdi ben "title:" sonra ilgili tüm kitaplar nesne göstermek anahtar kelime biraz maç ise bir şey aramak istediğinizMongoose'da bir bulma sorgusunu nasıl uygularım?

{ 
    "_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) 
} 

gibi MongoDB içinde Ebooks verilerin bir koleksiyona sahip.

Benim Gelincik şeması yer almaktadır: -

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); 

Denemek: -

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); 
     }); 
    }); 

ama ben nasıl yapabilirim boş bulundu? Sadece ilgili bir nesne bulduğumda biraz anahtar kelime aradığımda istiyorum.

cevap

3

Sorgunuzu sarmalarla sarmayı deneyin, Mongoose nesneyi sorgu olarak bekler.

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); 
     }); 
    }); 
İlgili konular