MongoDB聚合正则表达式匹配或全文搜索返回整个文档

时间:2015-04-17 18:02:22

标签: regex angularjs node.js mongodb mongoose

实施例。记录

[
  {
  "_id": "5528cfd2e71144e020cb6494",
  "__v": 11,
  "Product": [
                {
                "_id": "5528cfd2e71144e020cb6495",
                "isFav": true,
                "quantity": 27,
                "price": 148,
                "description": "100g",
                "brand": "JaldiLa",
                "name": "Grapes",
                "sku": "GRP"
                },
                {
                "_id": "552963ed63d867b81e18d357",
                "isFav": false,
                "quantity": 13,
                "price": 290,
                "description": "100g",
                "brand": "JaldiLa",
                "name": "Apple",
                "sku": "APL"
                }
            ],
  "brands": [
            "Whole Foods",
            "Costco",
            "Bee's",
            "Masons"
            ],
  "sku": "FRT",
  "name": "Fruits"
  }
]

我的Mongoose函数从AngularJS(http://localhost:8080/api/search?s=

返回查询
router.route('/search')
    .get(function(req, res) {
        Dept.aggregate(
                { $match: { $text: { $search: req.query.s } } }, 
                { $project : { name : 1, _id : 1,  'Product.name' : 1, 'Product._id' : 1} },
                { $unwind : "$Product" },
                { $group : {
                    _id : "$_id",
                    Category : { $addToSet : "$name"},
                    Product : { $push : "$Product"}
                }}
        )
    });

结果:例如http://localhost:8080/api/search?s=Apple /葡萄/胡萝卜,结果对所有人都一样。

[
  {
  "_id": "5528cfd2e71144e020cb6494",
  "Category": ["Fruits"],
  "Product": [
              {
              "_id": "5528cfd2e71144e020cb6495",
              "name": "Grapes"
              },
              {
              "_id": "552963ed63d867b81e18d357",
              "name": "Apple"
              },
              {
              "_id": "552e61920c530fb848c61510",
              "name": "Carrots"
              }
            ]
  }
]    

问题:在对“apple”的查询中,它返回Product中的所有对象而不仅仅是“grape”,我想可能在放松后放置匹配就可以完成诀窍或$ regex案例

我想要什么:例如搜索“葡萄”的搜索字符串 此外,我希望它在我发送查询的前两个字母后立即开始发送结果。

[{
  "_id": ["5528cfd2e71144e020cb6494"], //I want this in array as it messes my loop up
  "Category": "Fruits", //Yes I do not want this in array like I'm getting in my resutls
  "Product": [{
              "_id": "5528cfd2e71144e020cb6495",
              "name": "Grapes"
              }]
}]

感谢您的耐心等待。

1 个答案:

答案 0 :(得分:1)

使用以下聚合管道:

var search = "apple",
    pipeline = [
        {
            "$match": {
                "Product.name": { "$regex": search, "$options": "i" }  
            }
        },
        {
            "$unwind": "$Product"
        },
        {
            "$match": {
                "Product.name": { "$regex": search, "$options": "i" }
            }
        },
        {
            "$project": {
                "Category": "$name",
                "Product._id": 1,
                "Product.name": 1
            }
        }
    ];

    db.collection.aggregate(pipeline);

使用上面的示例文档和正则表达式(不区分大小写)在Product数组的name字段上搜索“apple”,上面的聚合管道产生结果:

<强>输出

/* 1 */
{
    "result" : [ 
        {
            "_id" : "5528cfd2e71144e020cb6494",
            "Product" : {
                "_id" : "552963ed63d867b81e18d357",
                "name" : "Apple"
            },
            "Category" : "Fruits"
        }
    ],
    "ok" : 1
}