0

I have this collection in a database, it contains a lot of documents on it, i would like all instances of "Æ" to be replaced with "ae".

My documents look like this:

{ 
    "_id" : ObjectId("57071e9fee31902f0a9ad989"),
    "layout" : "normal", 
    "name" : "Æther Flash", 
    "manaCost" : "{2}{R}{R}", 
    "cmc" : 4,
    "colors" : [ "Red" ]
}

I suppose i should use something like find_one_and_update, maybe?

4
  • All i could achieve so far is to locate all of them with db.getCollection('AllCards').find({'name':{'$regex':'Æ'}}) Commented Apr 28, 2016 at 22:59
  • Can you please show sample document? Commented Apr 28, 2016 at 23:18
  • { "_id" : ObjectId("57071e9fee31902f0a9ad989"), "layout" : "normal", "name" : "Æther Flash", "manaCost" : "{2}{R}{R}", "cmc" : 4, "colors" : [ "Red" ]} Commented Apr 28, 2016 at 23:20
  • Next time you should consider using the "edit" link on question to add additional informations. Commented Apr 28, 2016 at 23:24

1 Answer 1

3

This should work. It may take a while depending on how big you db is, which might be big considering its a MTG db ;), but it should work.

db.getCollection('AllCards').find({$or: [{name: {$regex: 'Æ'}}, {name: {$regex: 'æ'}}]}).forEach(function (x) {
    x.name = x.name.replace(/Æ/g, 'Ae').replace(/æ/g, 'ae');

    db.getCollection('AllCards').save(x);
});

Of course that only covers Æs in the name. But I suppose you can sort out how to make it work with all necessary fields.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.