0

I am in the process of migrating a database from Firebase to MongoDB and am facing a problem. Here is such an object I have stored in the FB:

"logs": {
    "2021-05-24": {
        "124109": {
            "foo": "text",
            "bar": "text"
        }
    }
}

When I import it into Mongo (through the Mongo compass as a JSON object) it is inserted in this form:

"logs": {
    "2021-05-24": [
        null,
        null,
        ... # overall 124109 nulls here
        null,
        {
            "foo": "text",
            "bar": "text"
        }
    ]
}

As I can see, it reads the key of the nested object as the number of real array objects. How can I correctly migrate an object of this type in mongo?

RoyalGoose
  • 115
  • 4

1 Answers1

1

Input JSON (input.json):

{
  "logs": {
    "2021-05-24": {
        "124109": {
            "foo": "text",
            "bar": "text"
        }
    }
  }
}

You can import using the mongoimport command-line tool. This will import into the MongoDB collection correctly. For example, from the OS command prompt:

mongoimport --db=test --collection=test --file=input.json

Note the "input.json" file needs to be in the same directory from where you are executing the command (or specify full path).

The resulting document looked like this, with the MongoDB created _id field:

{
        "_id" : ObjectId("613f25725ed44abe4b8957ef"),
        "logs" : {
                "2021-05-24" : {
                        "124109" : {
                                "foo" : "text",
                                "bar" : "text"
                        }
                }
        }
}

I had tried the same import using the MongoDB Compass v1.21.2 inot MongoDB v4.2.8 database - and I see the same problem as you are getting.

prasad_
  • 583
  • 2
  • 10