【问题标题】:JSON: Can't place ObjectIds of my mongodb database into mLab collection onlineJSON:无法将我的 mongodb 数据库的 ObjectIds 放入 mLab 在线集合中
【发布时间】:2018-03-31 10:43:39
【问题描述】:

所以我想将我电脑上的数据库的数据在线添加到mlab数据库中。我复制了数据(我假设是 JSON)并尝试将其粘贴到我的 mlab 数据库中的集合中。

现在,问题是我的数据库中的 ObjectIds 导致错误,因为它不是有效的 JSON。有谁知道如何解决这个问题?

示例文档

{
    "_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 错误

JSON 验证错误 close 我们在解析您的文件时遇到错误 JSON。请检查您的语法(例如,确保您使用的是 double 将您的字段名称和值都用引号引起来),然后重试。

我在 JSON lint 中测试了代码,它给了我这个错误:

Error: Parse error on line 2:

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

【问题讨论】:

    标签: json mongodb mlab


    【解决方案1】:

    mLab 上的数据编辑器使用严格的 MongoDB 扩展 JSON。详情见这里:docs.mongodb.com/manual/reference/mongodb-extended-json

    在严格的 MongoDB 扩展 JSON 中,ObjectId("<id>") 需要为 { "$oid": "<id>" }。试试这个:

    {
        "_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
    }
    

    【讨论】:

      猜你喜欢
      • 2018-09-17
      • 2011-09-08
      • 1970-01-01
      • 1970-01-01
      • 2017-06-05
      • 2019-07-11
      • 2023-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多