MongoDB dump from 3.2, restore with 3.4, error index safe = null

18,638

Solution 1

safe=true is not an index specification.

In previous versions of MongoDB, lower than 3.4, extra indexes specifications can be added. Those were used by specific drivers.

In 3.4, mongodb added a validation on indexes specification:

Ensuring that the specified index options are valid. Previous versions ignored invalid options.

That's why you have this error. I am afraid you need to ensure that the index in your 3.2 version does not have invalid index specificaitons, and after that do the mongodump.

As kz_sergey says in his answer, you can mongorestore using --noIndexRestore, that should work fine.

Solution 2

Why do you restore indexes? --noIndexRestore and create them again.

Solution 3

In the spirit of Aymeric's comment, you can use this awk one-liner to replace the "safe" property in your .metadata.json files.

awk -i inplace '{gsub(",\"safe\":null", ""); print}' *.metadata.json

Run it in the directory of your MongoDB export. This approach allows you to keep the indexes, but drop the "safe" option.

Solution 4

find . -type f -name "*.metadata.json" -exec sed -i 's/,"safe":null//g' {} \;

This works and you will keep your indexes! It find all files in the present location (.) then using the same process (exec) replace in file (sed -i) according to the following regex which is basically saying all occurrences of "safe":null with nothing.

Replace the "." argument with the path to the directory where your mongodb exports are stored.

Solution 5

Since I wanted to keep all indexes, and none of the methods above worked in my case, I've just edited all *.metadata.json files and manually removed all occurrences of "safe":true.

Context: I'm using an old database which I cannot upgrade to 4.x or above - it was originally running on 3.2, but there's no version of mongo running on new Macs with Apple Silicon, so I had to use 3.4 (the first version available) and do a dump from the old Mac and a restore from the new Mac.

Share:
18,638
Patrik Laszlo
Author by

Patrik Laszlo

Just my life is all about coding, hacking, servers and distributed systems.

Updated on July 14, 2022

Comments

  • Patrik Laszlo
    Patrik Laszlo almost 2 years

    I get the following error (dump MongoDB 3.2) (restore MongoDB 3.4):

    Failed: ngivr-dev.ledgerhelpers: error creating indexes for ngivr-dev.ledgerhelpers: **createIndex error:** **The field 'safe' is not valid for an index specification.** Specification: **{ unique: true, name: "ledgerId_1", safe: null, ns: "ngivr-dev.ledgerhelpers", background: true, key: { ledgerId: 1 } }**
    

    Looks like the safe index is null. But how can i use it with MongoDB 3.4? 3.2 is ok.