Mongorestore: createIndex error: Values in v:2 index key pattern cannot be of type object. Only numbers > 0, numbers < 0, and strings are allowed
Asked Answered
F

3

10

I receive the following error when running mongorestore command with a large MongoDB database.

the machine where the backup was done:

db version v4.2.0
git version: a4b751dcf51dd249c5865812b390cfd1c0129c30
OpenSSL version: OpenSSL 1.1.1  11 Sep 2018
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1804
    distarch: x86_64
    target_arch: x86_64

local machine version (where I execute mongorestore)

db version v4.0.10
git version: c389e7f69f637f7a1ac3cc9fae843b635f20b766
allocator: system
modules: none
build environment:
    distarch: x86_64
    target_arch: x86_64

Failed: prod.DeviceState: error creating indexes for prod.DeviceState: createIndex error: Values in v:2 index key pattern cannot be of type object. Only numbers > 0, numbers < 0, and strings are allowed.

Any help to solve this issue?

Thank you

Footling answered 7/1, 2020 at 16:44 Comment(0)
F
13

For the people who will come here in the future the issue was, I was using MongoDB 4.0 on the local and server was on 4.2:

Starting in version 4.2, mongodump uses Extended JSON v2.0 (Canonical) format for the metadata files. To parse these files for restore, use mongorestore version 4.2+ that supports Extended JSON v2.0 (Canonical or Relaxed mode) format. For details please see "Metadata Format" section on this : [docs.mongodb.com/manual/reference/program/mongodump][1]

source: https://dba.stackexchange.com/questions/250312/failed-to-restored-indexes-using-mongorestore-mongodb

Footling answered 7/1, 2020 at 17:2 Comment(0)
W
1

Starting in version 4.2, mongodump uses Extended JSON v2.0 (Canonical) format for the metadata files. To parse these files for restore, use mongorestore version 4.2+ that supports Extended JSON v2.0 (Canonical or Relaxed mode) format.

Tip
If general, use corresponding versions of mongodump and mongorestore. That is, to restore data files created with a specific version of mongodump, use the corresponding version of mongorestore.

https://docs.mongodb.com/manual/reference/program/mongodump/#metadata-format

So, you need to execute mongorestore with v4.2.0

Or, try this (keep MongoDB backup)

  1. Stop Mongod v4.2.0
  2. Run Mongod v4.0.10 (if fail, go next trick)
  3. mongodump database with v4.0.10

Other trick:

mongodump uses Extended JSON v2.0 (Canonical) format for the metadata files

  1. Start mongod v4.0.10
  2. Create all collections + indexes
  3. mongodump v4.0.10
  4. Replace metadata files .json dump v4.2.0 with v4.0.10
  5. mongoestore dump with v4.0.10
Weightless answered 7/1, 2020 at 17:4 Comment(0)
J
0

Alternatively, it's possible to adjust the dumped json using the following:

sed -i -E 's/\{"\$(\w+)":"([+-]?[0-9]+([.][0-9]+)?)"\}/\2/g' XXX.metadata.json
Jordaens answered 6/4, 2023 at 15:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.