如何使用MongoDB从字符串中提取仅以数字开头的数字

时间:2016-06-21 09:20:52

标签: mongodb mapreduce mongodb-query

如何从MongoDB中以数字开头的字符串中提取唯一的数字

我想从以数字开头的字符串中提取数字。

我有一个包含

等文档的集合
  {
    "address" :"1103 wood crest"
  },
  {
    "address": "223 NW 28TH ST"
  },
  {
    "address": "NW 28TH ST"
  },
  {
    "address": "28TH ST ava"
   }

我想编写聚合查询,其结果如下:

{
  "number": "1103",
  "address" :"wood crest"
},
{
  "number": "223",
  "address": "223 NW 28TH ST"
},
{
  "number": "",
  "address": "NW 28TH ST"
},
{
   "number": "",
   "address": "28TH ST ava"
 }

1 个答案:

答案 0 :(得分:0)

嗯,你不能用聚合框架做到这一点。这里最好的选择是mapReduce

db.coll.mapReduce(
    function() { 
        var fields = {}; 
        fields["number"] = parseInt(this.address.match(/^\d+?\s+/)) || "";
        fields["address"] =  this.address; 
        emit(this._id, fields); 
    }, 
    function(key, value) {}, 
    { "out": { "inline": 1 } }
)

返回如下内容:

{
        "results" : [
                {
                        "_id" : ObjectId("57691c0206c5c92152979d3e"),
                        "value" : {
                                "number" : 1103,
                                "address" : "1103 wood crest"
                        }
                },
                {
                        "_id" : ObjectId("57691c0206c5c92152979d3f"),
                        "value" : {
                                "number" : 223,
                                "address" : "223 NW 28TH ST"
                        }
                },
                {
                        "_id" : ObjectId("57691c0206c5c92152979d40"),
                        "value" : {
                                "number" : "",
                                "address" : "NW 28TH ST"
                        }
                },
                {
                        "_id" : ObjectId("57691c0206c5c92152979d41"),
                        "value" : {
                                "number" : "",
                                "address" : "28TH ST ava"
                        }
                }
        ],
        "timeMillis" : 25,
        "counts" : {
                "input" : 4,
                "emit" : 4,
                "reduce" : 0,
                "output" : 4
        },
        "ok" : 1
}