Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

MongoDB update statement to remove a portion of text in a field

I have a mongoDB collection with a field called "Url". There are many records in this collection. Inside this field, it always contains this text below:

"http://image-assets.s3.us-west-2.amazonaws.com/" For example "http://image-assets.s3.us-west-2.amazonaws.com/folder1/folder2/blah.jpg"

I would like to remove the portion of "http://image-assets.s3.us-west-2.amazonaws.com/", so the final result should look like "folder1/folder2/blah.jpg"

I would like to write a mongodb update statement, I don't know where to start. I can think of two things: (a) remove the first x number of characters, or (b) replace the text with a null. However the text includes a lot of slashes (/)

Your help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This aggregation query with update on the result will work:

db.urls.aggregate( [ 
  { 
      $project: { 
          url: { 
              $split: [ 
                  "$url", 
                  "http://image-assets.s3.us-west-2.amazonaws.com/"
              ] 
          }
      }
  } 
] ).forEach( doc => db.urls.updateOne( { _id: doc._id }, { $set: { url: doc.url[1] } } )

With these two input documents:

{ url: "http://image-assets.s3.us-west-2.amazonaws.com/folder1/folder2/blah.jpg" }
{ url: "http://image-assets.s3.us-west-2.amazonaws.com/folder1/folder2/blah2.jpg" }

The updated url field will have values:

folder1/folder2/blah.jpg
folder1/folder2/blah2.jpg



[ EDIT ADD ]

Adding a check for url field with the URL string to be updated. Add this $match stage before the $project stage in the above aggregation query.

Note that in the regex search string ^ means "starts with", and the . (dot) is prefixed with a (backslash) so that it is considered as a dot only (not as regex meta-character).

  { 
      $match: { 
          url: { $regex: '^http://image-assets.s3.us-west-2.amazonaws.com/' } 
      } 
  }, 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...