Mongodb:在聚合管道中拆分字符串

Mongodb:在聚合管道中拆分字符串,mongodb,aggregation-framework,Mongodb,Aggregation Framework,这是已存储到集合中的我的文档: { "_id" : UUID("61a2053c-1a79-4649-8793-df6c4dc1973"), "NotificationId" : UUID("ad068e4e-10e2-528c-a74a-df6c4dd9211"), "DistributionId" : UUID("f5445ea1-e6cb-4acd-9881-c4122df6c4d"), "CreationDateTime" : ISODate("2016-

这是已存储到集合中的我的文档:

{
    "_id" : UUID("61a2053c-1a79-4649-8793-df6c4dc1973"),
    "NotificationId" : UUID("ad068e4e-10e2-528c-a74a-df6c4dd9211"),
    "DistributionId" : UUID("f5445ea1-e6cb-4acd-9881-c4122df6c4d"),
    "CreationDateTime" : ISODate("2016-07-13T04:20:38.697Z"),
    "ExpirationDateTime" : ISODate("2099-01-01T00:00:00.000Z"),
    "DeliveryType" : 1,
    "DeliveryParams" : [],
    "Address" : "Topics/Messages/Global",
    "Payload" : "{\"Id\":\"ad067896-10e2-528c-er87-df6c4d123654\",\"CreationDateTime\":\"\\/Date(1468324824751)\\/\",\"DeviceId\":\"456987456985\",\"UserId\":\"64545678-1234-4834-4321-123456789012\",\"UserFullName\":\"test-user\",\"SystemId\":\"com.messaging\",\"SystemTitle\":\"message\",\"EventId\":\"messaging.message\",\"EventTitle\":\"ارسال پیام\",\"EventData\":[],\"BusinessCode\":\"1-2-4-4-5-6-9\",\"ProcessId\":\"55333333-4433-3333-7733-113333333399\",\"WorkItemId\":423458,\"WKT\":\"\"}",
    "SendAttempts" : null,
    "Sent" : ISODate("2016-11-10T10:01:22.140Z"),
    "Delivered" : ISODate("1970-01-01T00:00:00.000Z")
}
我的问题是,如何将\BusinessCode\:\1-2-4-4-5-6-9\拆分到内部 有效载荷字段。我只需要业务代码:1-2-4-4-5-6-9,用于将门店转入其他字段

我使用了以下脚本:

db.Messages.find().forEach(function(item)
{
    id = item._id;
    payload = item.Payload;
    matched = payload.match(/\"BusinessCode\":\"(([1-2]?[0-9])-([0-9]*)-([0-9]*)-([0-9]*)-([0-9]*)-([0-9]*)-([0-9]*))\"/);
....
db.Messages.updateOne(
....
这是有效载荷。匹配返回

此用于收集多个文档的脚本不合适,并且速度较慢。我想使用聚合管道


我怎样才能在聚合管道中得到与payload.match完全相同的响应?

从3.6.2开始,mongo中没有regexp子字符串

我们可以使用substr方法对业务id进行子串,因为有效负载包含unicode字符,所以我们需要使用CodePoint CP来获得预期的结果

db.col.aggregate(
    [
        {$addFields : {
                start : {$indexOfCP : ["$Payload", "BusinessCode"]},
                end : { $indexOfCP : ["$Payload", "ProcessId"]}
            }
        },
        {$project : {BusinessCode : {$substrCP : ["$Payload", {$sum : ["$start",15]}, {$subtract : [{$subtract : ["$end", "$start"]}, 18]}]}}}
    ]
)
结果

{ "_id" : "61a2053c-1a79-4649-8793-df6c4dc1973", "BusinessCode" : "1-2-4-4-5-6-9" }
{ "_id" : "61a2053c-1a79-4649-8793-df6c4dc1973", "BusinessCode" : "1-2-4-4-5-6-9" }