【问题标题】:MongoDB: Filter Fullname by combination of firstname and lastnameMongoDB:通过名字和姓氏的组合过滤全名
【发布时间】:2018-07-31 19:00:23
【问题描述】:

示例文档如下

{
    "_id" : ObjectId("5a61cbc677366f5e0cdc95db"),
    "Indvl" : {
        "Info" : {
            "lastNm" : "ALEX",
            "firstNm" : "BOB",
            "midNm" : "Y",
        }       
    }
}

现在我们使用以下查询来使用名称进行搜索。 我们必须对lastNmfirstNmmidNm 的组合做同样的事情

db.getCollection("MyCOL").find({ "Indvl.Info.firstNm":{$regex: "al" , $options:"i"})

我们该怎么做?

【问题讨论】:

  • @Veeram 它不是重复的,我有更多过滤器要添加到同一个查询中,所以我不能使用函数,聚合。
  • 请更新您的问题以澄清。
  • @Veeram 感谢您没有帮助和破坏它。

标签: mongodb nosql


【解决方案1】:
db.MyCOL.aggregate(

    // Pipeline
    [
        // Stage 1
        {
            $project: {
                name: {
                    $concat: ['$Indvl.Info.lastNm', ' ', '$Indvl.Info.firstNm', ' ', '$Indvl.Info.midNm']
                }
            }
        },

        // Stage 2
        {
            $match: {
                name: /al/i
            }
        },

    ]



);

【讨论】:

    【解决方案2】:

    这回答了我:

    db.MyCOL
        .aggregate(
            [ { $project: {fullName: { $concat: ['$Indvl.Info.firstNm', ' ', '$Indvl.Info.lastNm']}} },
              { $match: {fullName: /alex bob/} }
            ]
    );
    

    【讨论】:

      猜你喜欢
      • 2015-09-01
      • 2020-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-22
      • 2012-11-20
      • 1970-01-01
      相关资源
      最近更新 更多