mongodb 字符串 截取_使用正则表达式从MongoDB中提取子字符串列表

I need to extract a part of a string that matches a regex and return it.

I have a set of documents such as:

{"_id" :12121, "fileName" : "apple.doc"},

{"_id" :12125, "fileName" : "rap.txt"},

{"_id" :12126, "fileName" : "tap.pdf"},

{"_id" :12126, "fileName" : "cricket.txt"},

I need to extract all file extensions and return {".doc", ".txt", ".pdf"}.

I am trying to use the $regex operator to find the sub strings and aggregate on the results but am unable to extract the required part and pass it down the pipeline.

I have tried something like this without success:

aggregate([

{ $match: { "name": { $regex: '/\.[0-9a-z]+$/i', "$options": "i" } } },

{ $group: { _id: null, tot: { $push: "$name" } } }

])

解决方案

It will be possible to do this in the upcoming version of MongoDB(as the time of this writing) using the aggregation framework and the $indexOfCP operator. Until then, your best bet here is MapReduce.

var mapper = function() {

emit(this._id, this.fileName.substring(this.fileName.indexOf(".")))

};

db.coll.mapReduce(mapper,

function(key, value) {},

{ "out": { "inline": 1 }}

)["results"]

Which yields:

[

{

"_id" : 12121,

"value" : ".doc"

},

{

"_id" : 12125,

"value" : ".txt"

},

{

"_id" : 12126,

"value" : ".pdf"

},

{

"_id" : 12127,

"value" : ".txt"

}

]

For completness here is the solution using the aggregation framework*

db.coll.aggregate(

[

{ "$match": { "name": /\.[0-9a-z]+$/i } },

{ "$group": {

"_id": null,

"extension": {

"$push": {

"$substr": [

"$fileName",

{ "$indexOfCP": [ "$fileName", "." ] },

-1

]

}

}

}}

])

which produces:

{

"_id" : null,

"extensions" : [ ".doc", ".txt", ".pdf", ".txt" ]

}

*current development version of MongoDB (as the time of this writing).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值