【问题标题】:How to extract only number from string which start with number using MongoDB如何使用MongoDB仅从以数字开头的字符串中提取数字
【发布时间】:2016-06-21 09:20:51
【问题描述】:

如何从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"
 }

【问题讨论】:

    标签: mongodb mapreduce mongodb-query


    【解决方案1】:

    嗯,你不能用聚合框架来做到这一点。你最好的选择是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
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-15
      • 1970-01-01
      • 2021-01-08
      • 1970-01-01
      • 1970-01-01
      • 2022-08-13
      • 2021-07-18
      • 2012-06-15
      • 1970-01-01
      相关资源
      最近更新 更多