Mongoose全文搜索返回空数组

时间:2016-02-15 03:08:35

标签: node.js search text mongoose

我有跟随架构和子架构,我想我已经从mongoose文件中执行了所有步骤,但我总是得到空数组。

// other libraries

var mongoose = require('mongoose');
var schema = mongoose.Schema;


    // product schema
    var productSchema = new schema({
        title: {type: String},
        ingredients: {type: String},
        description: {type: String},
        banner: [{path: String, fileName: String}],
        price: {type: String},
        promo: {promotion: {type: Boolean, default: 0}, promoDetail: {type: String, default: null}},
        stock: {type: Number, default: 0},
        tags: [],
        sold: {type: Number, default: 0},
        pickOfTheDay: {type: Boolean, default: 0},
        shipping: {type: String, default: null},
        featured: {type: Boolean, default: 0},
        showCounter: {type: Number, default: 0},
        createdAt: {type: Date, default: Date.now},
        updatedAt: {type: Date, default: Date.now},
        seller: {}
    });




    // merchant schema
    var Schema = new schema({
        accountName: {type: String},
        firstName: {type: String},
        middleName: {type: String},
        lastName: {type: String},
        address: {  street: {type: String}, 
                    city: {type: String}, 
                    province: {type: String}, 
                    country: {type: String}, 
                    postalCode: {type: String}
                },
        phone: { phone1: {type: Number},
                 phone2: {type: Number}
                },
        email: {type: String},
        userType: {type: String, default: "customer"},
        password: {type: String},
        socialAccounts: {   facebook: {id: String, token: String, email: String, name: String}, 
                            twitter: {id: String, token: String, displayName: String, username: String}, 
                            google: {id: String, token: String, email: String, name: String}
                        },
        products: [productSchema]
    });




    // creating indexes
    Schema.index({
            'products.title': "text",
            'products.ingredients': "text",
            'products.description': "text"
    });



    var model = mongoose.model('merchant', Schema);


exports.modelExports = function(){
    return model;
}

这里我的收藏品上有以下文件

{ 
    "_id" : ObjectId("56b06b7fe61af1081ceea185"), 
    "accountName" : "Agnes", 
    "firstName" : "Agnes", 
    "middleName" : "Monica", 
    "lastName" : "AgnesMo", 
    "email" : "agnes@yahoo.com", 
    "password" : "$2a$10$wpHVqyw4sFG1.MwtQj6XPOdx5AJ/kyi92I1Z50GrWMlhI97E3ZrpW", 
    "products" : [
        {
            "title" : "Sate Maranggi", 
            "ingredients" : "Daging sapi, kecap, cabai, bawang merah, bawang putih", 
            "description" : "Sate maranggi enak dan lezat", 
            "price" : "15000", 
            "_id" : ObjectId("56b06eae492acbbc1fd070e9"), 
            "pickOfTheDay" : false, 
            "sold" : NumberInt(0), 
            "featured" : true, 
            "tags" : [
                "Sate Maranggi"
            ], 
            "stock" : NumberInt(20), 
            "promo" : {
                "promoDetail" : "Beli 2 gratis 1", 
                "promotion" : true
            }, 
            "banner" : [
                {
                    "path" : "D:\\project\\storage\\uploads\\images\\Resep-Cara-Membuat-Sate-Maranggi-Khas-Purwakarta.jpg-1454403246620.jpg", 
                    "fileName" : "Resep-Cara-Membuat-Sate-Maranggi-Khas-Purwakarta.jpg-1454403246620.jpg", 
                    "_id" : ObjectId("56b06eae492acbbc1fd070ea")
                }
            ]
        }
    ], 
    "userType" : "merchant", 
    "phone" : {
        "phone1" : NumberInt(88888), 
        "phone2" : NumberInt(999999)
    }, 
    "address" : {
        "street" : "jl. A.Yani no 23", 
        "city" : "Jakarta", 
        "province" : "DKI Jakarta", 
        "country" : "indonesia", 
        "postalCode" : "61382"
    }, 
    "__v" : NumberInt(1)
}

这是我在另一个文件中的搜索查询

// call the merchant model
var merchantM = require(__base + 'app/models/merchant_m');
var model = merchantM.modelExports();

    // text search
    model.find({ $text: { $search: "Sate" } }).exec(function(err, docs){
            if (err){
                res.send(err);
            }else{
                res.send(docs);
            }

然后返回一个空数组。 我会错过什么吗?

1 个答案:

答案 0 :(得分:1)

请确保您要查询的集合名称在MongoDB中为merchant,与您在下面声明的型号名称相匹配。

var model = mongoose.model('merchant', Schema);
相关问题