簡體   English   中英

如何在Mongoose中執行查找查詢?

[英]How do I perform a find query in Mongoose?

我有mongodb中的Ebooks數據集合

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

現在我想搜索一些東西,如果關鍵字與"title:"有點匹配,則顯示所有相關的書籍對象。

我的Mongoose架構是: -

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

我試試: -

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

但我發現null怎么辦? 我只想在搜索一點點關鍵字時找到所有相關對象。

嘗試用curlies包裝你的查詢,Mongoose期望一個對象作為查詢。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM