tilly
tilly

Reputation: 2610

JSON: Can't place ObjectIds of my mongodb database into mLab collection online

So I would like to add the data of my database on my computer to mlab database online. I copied the data (which I assumed was JSON) and tried to paste it into the collection in my mlab database.

Now, the problem is that the ObjectIds in my database are causing an error because it is not valid JSON. Does anyone know how to fix this?

Example document

{
    "_id" : ObjectId("5a8ae78844a5ba0d448a4cf7"),
    "ratings" : [],
    "reviews" : [],
    "title" : "The Shape of Water",
    "director" : "Guillermo Del Toro",
    "length" : 123,
    "genre" : "Drama",
    "description" : "At a top secret research facility in the 1960s, a lonely janitor forms a unique relationship with an amphibious creature that is being held in captivity.",
    "actors" : "French chick, Fish Dude",
    "year" : 2018,
    "pictureUrl" : "https://media.pathe.nl/nocropthumb/620x955/gfx_content/other/api/filmdepot/v1/movie/download/23452_94164_ps_sd-high.jpg",
    "__v" : 18,
    "averageRating" : 4
}

Mlab Error

JSON Validation Error close We encountered an error while parsing your JSON. Please check your syntax (e.g. ensure you are using double quotes around both your field names and values) and try again.

I tested the code in JSON lint and it gave me this error:

Error: Parse error on line 2:

{   "_id": ObjectId("5a69cc1f90
---------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

Upvotes: 0

Views: 239

Answers (1)

tfogo
tfogo

Reputation: 1436

The data editor on mLab uses strict MongoDB Extended JSON. See here for details: docs.mongodb.com/manual/reference/mongodb-extended-json

In strict MongoDB Extended JSON ObjectId("<id>") needs to be { "$oid": "<id>" }. Try this:

{
    "_id" : { 
        "$oid": "5a8ae78844a5ba0d448a4cf7" 
    },
    "ratings" : [],
    "reviews" : [],
    "title" : "The Shape of Water",
    "director" : "Guillermo Del Toro",
    "length" : 123,
    "genre" : "Drama",
    "description" : "At a top secret research facility in the 1960s, a lonely janitor forms a unique relationship with an amphibious creature that is being held in captivity.",
    "actors" : "French chick, Fish Dude",
    "year" : 2018,
    "pictureUrl" : "https://media.pathe.nl/nocropthumb/620x955/gfx_content/other/api/filmdepot/v1/movie/download/23452_94164_ps_sd-high.jpg",
    "__v" : 18,
    "averageRating" : 4
}

Upvotes: 1

Related Questions