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?

6
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community
    Sep 11 at 12:55
  • @Community I put the source data in upper codeblock, which contain the object "124109": { ... }, but after importing to mongo it transforming into array [ ], my question is how to fix it
    – RoyalGoose
    Sep 11 at 13:04
  • Explain the process you had used - in detail - how you have imported into MongoDB Noted the mention of Compass, that is not the complete process you are trying.
    – prasad_
    Sep 13 at 4:31
  • @prasad_ i exported db from firebase to json file, next I opened the mongodb compass, opened the collection, pressed import via json file and imported
    – RoyalGoose
    Sep 13 at 8:28
  • What does the exported json look like?
    – prasad_
    Sep 13 at 9:19
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.

1
  • 1
    Thanks for the answer. Yes, indeed, if I import the data in any other way, except through Mongo Compass, everything works (checked in pymongo). Apparently there is some problem in Mongo Compass import service.
    – RoyalGoose
    Sep 13 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.