Mongo find by regex: return only matching string

前端 未结 2 1070
天命终不由人
天命终不由人 2021-01-14 15:13

My application has the following stack:

Sinatra on Ruby -> MongoMapper -> MongoDB

The application puts several entries in the database. In order to crosslink

相关标签:
2条回答
  • 2021-01-14 16:01

    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.

    0 讨论(0)
  • 2021-01-14 16:08

    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

    0 讨论(0)
提交回复
热议问题