Hoa
Hoa

Reputation: 20438

How can I do a find and replace all in MongoDB?

For example suppose I insert data as follows

doc1 = [{url: 'http://domain.com/pic1.jpg'}, {url: 'http://domain.com/pic2.jpg'}]
doc2 = [{url: 'http://domain.com/pic3.jpg'}, {url: 'http://domain.com/pic4.jpg'}]
db.picture.insert(doc1)
db.picture.insert(doc2)

How could I replace all 'http' with 'https'?

Upvotes: 4

Views: 2672

Answers (1)

Ian Daniel
Ian Daniel

Reputation: 606

MongoDB does not have in-built support for search and replace of a portion of a string. You could write a program in your favourite scripting language to do this.

You can use regular expression searching to get back all the URLs that start with "http:":

db.picture.find({url: /^http:/})

You could do that in your program to get the data, then modify it in your program, and update or replace the documents with the new values.

Upvotes: 5

Related Questions