Mongo find by regex: return only matching string

随声附和 提交于 2019-12-01 20:43:30

I don't know if I understand.

db.yourColl.aggregate([
{
    $match:{"yourKey":{$regex:'[a-zA-Z0-9]', "$options" : "i"}}
},
{
    $group:{
        _id:null,
        tot:{$push:"$yourKey"}
    }
}])

If you don't want to have duplicate in totuse $addToSet

Ganesh

The way I solved this problem is using the string aggregation commands to extract the StartingIndexCP, ending indexCP and substrCP commands to extract the string I wanted. Since you could have multiple of these {} you need to have a projection to identify these CP indices in one shot and have another projection to extract the words you need. Hope this helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!