【问题标题】:MongoDB Aggregate Regex Match or Full Text Search returns whole DocumentMongoDB 聚合正则表达式匹配或全文搜索返回整个文档
【发布时间】:2015-06-24 17:30:58
【问题描述】:

例如。记录

[
  {
  "_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 中的所有对象,而不仅仅是“grapes”,我认为也许在展开之后放置 match 可以解决问题或 $regex case

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

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

感谢您的耐心等待。

【问题讨论】:

  • UPDATE: { $match : { '$idol.name' : new RegExp(query, 'gi') } } 给出一个键查询,现在唯一剩下的就是特定对象只返回
  • UPDATE: { $sort: { score: { $meta: "textScore" }, name: 1 } }, { $limit: 1 } 它返回整个文档
  • 我不知道是否可以解决问题,我尽力了..如果缺少任何必需的信息,请告诉我。 @JonathanMee

标签: regex angularjs node.js mongodb mongoose


【解决方案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
}

【讨论】:

  • 谢谢,所以 $group 不是必需的..希望我早点知道..可以救我 3 天
  • @AyushSingh 当您想根据某些字段对文档进行分组并组合它们的值时,这是必要的。在您的情况下,您不需要任何分组,而是需要根据特定条件对某些字段进行一些投影。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2018-06-13
  • 2019-09-07
  • 2020-11-02
  • 1970-01-01
  • 2020-05-20
相关资源
最近更新 更多