如何替换短语mongodb的多次出现 [英] how to replace multiple occurrences of a phrase mongodb

查看:43
本文介绍了如何替换短语mongodb的多次出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试替换同一字段中同一短语的多次出现.

I am trying to replace multiple occurrences of the same phrase in the same field.

以下是"imageUrl"字段内容的示例:

Here is an example of the field 'imageUrl' contents:

"imageUrl":" https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_2000x.png?v=1576212956 \ n\ n \ n"

"imageUrl" : "https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_2000x.png?v=1576212956\n \n https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_2000x.png?v=1576212956\n \n \n"

我当前的公式:

  {
      $match: {
           imageUrl: { $regex: "_2000x" },
           brand: "Goat The Label"
      }
  },
  { 
      $addFields: { 
          imageUrl: { $split: [ "$imageUrl", "_2000x" ] } 
      } 
  },
  { 
      $addFields: { 
          imageUrl: { 
              $concat: [ 
                        { $arrayElemAt: [ "$imageUrl", 0 ] }, 
                        "_850x", 
                        { $arrayElemAt: [ "$imageUrl", 1 ] }
              ] 
          }
      }
 }]).forEach( doc => db.product.updateOne( { _id: doc._id }, { $set: { imageUrl: doc.imageUrl } } ) )

将以下内容转换为:

"imageUrl":"

"imageUrl" : "https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_850x.png?v=1576212956\n \n \n"```

但是,我正在寻找输出为:

However, im looking for the output to be:

"imageUrl":" https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_850x.png?v=1576212956 \ n\ n \ n"

"imageUrl" : "https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_850x.png?v=1576212956\n \n https://cdn.test.com/s/files/1/0035/4671/0061/products/Screen_Shot_2019-12-13_at_12.47.01_pm_850x.png?v=1576212956\n \n \n"

这两个网址仍然存在,并已适应 _850x 维度.

Both urls still present and adapted to the _850x dimension.

注意:字段输入可能超过一个网址(可能包含2-7).

Note: The field input may be more then one Url (could contain 2-7).

在此先感谢您的帮助

推荐答案


db.collection.aggregate([
  {
    $match: {
      imageUrl: {
        $regex: "_2000x"
      },
      brand: "Goat The Label"
    }
  },
  {
    $addFields: {
      imageUrl: {
        $reduce: {
          input: {
            $slice: [
              {
                $split: [
                  "$imageUrl",
                  "_2000x"
                ]
              },
              1,
              {
                $size: {
                  $split: [
                    "$imageUrl",
                    "_2000x"
                  ]
                }
              }
            ]
          },
          initialValue: {
            $arrayElemAt: [
              {
                $split: [
                  "$imageUrl",
                  "_2000x"
                ]
              },
              0
            ]
          },
          in: {
            $concat: [
              "$$value",
              "_850x",
              "$$this"
            ]
          }
        }
      }
    }
  }
])

MongoPlayground

编辑:通过Bash shell执行:

To execute via Bash shell:

Windows:

Windows:

使用以下命令创建temp.js文件:

create temp.js file with:

db.collection.aggregate(...).forEach(...)

使用以下命令创建temp.bat文件:

create temp.bat file with:

@echo off

path/to/mongo.exe --uri put_here_mongodb+srv temp.js

现在运行: temp.bat

Unix:

Unix:

使用以下命令创建temp.js文件:

create temp.js file with:

db.collection.aggregate(...).forEach(...)

使用以下命令创建temp.bat文件:

create temp.bat file with:

#!/bin/bash

path/to/mongo --uri put_here_mongodb+srv temp.js

授予执行权限: chmod u + x temp.sh
现在运行: ./temp.sh

这篇关于如何替换短语mongodb的多次出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆