10
votes

I have many mongodb instance files like below;

[
{ 
    "_id" : ObjectId("35d455de983c0e6a53ea0848"), 
    "createdAt" : ISODate("2019-12-05T23:25:04.347+0000"), 
    "__v" : NumberInt(0)
},
{ 
    "_id" : ObjectId("1ecbe0f75df8ccd52a7b1662"), 
    "createdAt" : ISODate("2019-12-17T12:40:53.521+0000"), 
    "__v" : NumberInt(0)
}
]

I couldn't import these files because of invalid format.

mongoimport --db DATABASENAME --collection COLLECTIONNAME --file filename.json --jsonArray

And it says;

Failed: invalid JSON input. Position: 16. Character: O

Is there any other way to import those files?

If not, how can I convert them to be imported?

3
That is not valid json you are trying to import. What happens when you remove the ending --jsonArray? - Pytth
mongoimport is rum from operating system command prompt. The JSON data and the import command used above are fine. - prasad_

3 Answers

20
votes

you can use --legacy option of mongoimport in order to import the file with the format you want with ObjectId("an oid"), NumberInt(an int) or new Date("an iso date" or any mongo extension available in extended json v1 : https://docs.mongodb.com/manual/reference/mongodb-extended-json-v1/)

3
votes

Solution 1 (semi-auto):

We can import database only in json/csv format so remove all invalid characters such as ObjectId, ISODate, NumberInt, ()...

Solution2 (auto):

Finally, I found an alternative solution.

We can't import such data even in Mongo Compass.

But fortunately, we can import/export database in various formats in Studio 3T for MongoDB.

I think Studio 3T is better than Mongo Compass, but it is not free. After 30 days, we can't use Studio 3T trial version.

I will be very thankful if you let me know a way to reset Studio 3T.

0
votes

I had same problem, the error was about a property called '$init' that was added by mongoose automatically. I removed all those by Sublime and imported again by 3T.