在子文档上使用mongoose全文搜索并仅返回匹配子

时间:2016-02-16 02:28:38

标签: node.js mongodb search mongoose

你好我有问题通过文本搜索子文档,我一直在使用查询$ text:{$ search:" string"},但它返回包含父文件的整个文档

这是我的收藏品

{ 
    "_id" : ObjectId("56b06b7fe61af1081ceea185"), 
    "accountName" : "Agnes",
    "address" : {
        "street" : "jl. A.Yani no 23", 
        "city" : "Jakarta", 
        "province" : "DKI Jakarta", 
        "country" : "indonesia", 
        "postalCode" : "61382"
    }, 
    "__v" : NumberInt(1),
    "products" : [
        {
            "title" : "Ayam goreng kremes", 
            "ingredients" : "Daging ayam, tepung terigu, bawang merah, sasa", 
            "description" : "", 
            "price" : "15000", 
            "_id" : ObjectId("56c191bd3da50ccc149717a5"), 
            "updatedAt" : ISODate("2016-02-15T08:52:13.465+0000"), 
            "createdAt" : ISODate("2016-02-15T08:52:13.465+0000"), 
            "showCounter" : NumberInt(0), 
            "featured" : false
        }, 
        {
            "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
        }
    ]
}
{ 
    "_id" : ObjectId("56c190203da50ccc149717a3"), 
    "accountName" : "acha",
    "address" : {
        "street" : "jl. Sudirman no 23", 
        "city" : "Bogor", 
        "province" : "Jawa Barat", 
        "country" : "indonesia", 
        "postalCode" : "61382"
    }, 
    "__v" : NumberInt(1)
    "products" : [
        {
            "title" : "Pecel lele", 
            "ingredients" : "Lele, bawang merah, bawang putih, sasa, ketumbar, kemiri", 
            "description" : "", 
            "price" : "15000", 
            "_id" : ObjectId("56c192aa3da50ccc149717a9"), 
            "updatedAt" : ISODate("2016-02-15T08:56:10.716+0000"), 
            "createdAt" : ISODate("2016-02-15T08:56:10.716+0000"), 
            "showCounter" : NumberInt(0), 
            "featured" : false
        }
    ]
}

这是我的搜索查询

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

我需要的是匹配字符串的子文档" Sate"和父ID如果可能的话。

也许看起来像这样

{
    "parentID": ObjectId("56b06b7fe61af1081ceea185"),
    "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
}

0 个答案:

没有答案