【问题标题】:performing priority query in mongo在mongo中执行优先级查询
【发布时间】:2016-04-16 21:48:24
【问题描述】:

示例文档:

{"name":"John", "age":35, "address":".....",.....}
  1. join_month=3 优先级为 1 的员工
  2. 地址中包含字符串“Avenue”的员工优先级为 2
  3. 地址包含字符串“Street”的员工优先级为 3
  4. 地址中包含字符串“Road”的员工优先级为4

到目前为止,我正处于这个阶段:

db.collection.aggregate([
    { "$match": { 
        "$or": [ 
            { "join_month": 3 }, 
            { "address": /.*Avenue.*/i }, 
            { "address": /.*Street.*/i }, 
            { "address": /.*Road.*/i }
        ] 
    }}, 
    { "$project": { 
        "name": 1, 
        "age": 1,
        "_id": 0, 
        "priority": { ?????????? } 
    }}, 
    { "$sort":  { "priority": 1 } }
])

我被困在优先领域。我应该放什么?

【问题讨论】:

    标签: regex mongodb mapreduce mongodb-query


    【解决方案1】:

    使用聚合框架,您“理想地”希望在 $project 管道步骤,但不幸的是 MongoDB 尚未支持这一点。 目前开放的有一张 JIRA 票证$project filter using $regex

    但是,一种解决方法(虽然不是性能方面的最佳解决方案)是使用 map-reduce。考虑填充测试集合:

    db.test.insert([
        { _id: 0, "join_month": 12, "address": "33 Point Avenue", "name": "John", "age":35 },
        { _id: 1, "join_month": 10, "address": "2A Broad Street, Surbub", "name": "Jane", "age":21 },
        { _id: 2, "join_month": 3, "address": "127 Umpstreeten Road, Surbub", "name": "Alan", "age":63 },
        { _id: 3, "join_month": 3, "address": "127 Umpstreeten Road, Surbub", "name": "Louise", "age":30 }
    ])
    

    将地图函数定义为:

    var mapper = function() {
        var priority;
        if (this.join_month==3){
            priority = 1;
        }
        else if (this.address.match(/Avenue/i)){
            priority = 2;
        }
        else if (this.address.match(/Street/i)){
            priority = 3;
        }
        else if (this.address.match(/Road/i)){
            priority = 4;
        }
        else {
            priority = 99;
        }
    
        var value = {
            "name": this.name, 
            "age": this.age,
            "priority": priority
        };
        emit( this._id, value );        
    };
    

    reduce 函数如下:

    var reducer = function() { };
    

    然后在测试集合上运行mapduce操作,并将结果存入集合mr_result

    db.test.mapReduce(mapper, reducer, {
        "out": 'mr_result'
        "query": {
            "$or": [ 
                { "join_month": 3 }, 
                { "address": /.*Avenue.*/i }, 
                { "address": /.*Street.*/i }, 
                { "address": /.*Road.*/i }
            ] 
        }
    })
    

    查询结果集合:

    db.mr_result.find().sort({ "priority": 1})
    

    样本输出

    { "_id" : 2, "value" : { "name" : "Alan", "age" : 63, "priority" : 1 } }
    { "_id" : 3, "value" : { "name" : "Louise", "age" : 30, "priority" : 1 } }
    { "_id" : 0, "value" : { "name" : "John", "age" : 35, "priority" : 2 } }
    { "_id" : 1, "value" : { "name" : "Jane", "age" : 21, "priority" : 3 } }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-17
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 2010-09-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-11
      相关资源
      最近更新 更多