I
I
Ivan Yakushenko2019-07-03 15:02:20
MongoDB
Ivan Yakushenko, 2019-07-03 15:02:20

How to do multiple search and replace in mongodb?

The database has string objects like this: "1.950", "22.451", "1.140"...
These numbers should represent thousands, converting from $toInt gives me "1", "22", "1" and I need " 1950", "22451", "1140".
I would be fine with $replaceOne() if this function changed more than 1 first occurrence.
Can I somehow substitute a regular expression in update() set()? Or maybe there is some other way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2019-07-03
@kshnkvn

Rake-rake-crutches:

string_name: {$toInt: {$arrayElemAt: [{$split: ["$string_to_replace", "."]}, 0]}}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question