Importing json from file into mongodb using mongoimport

后端 未结 3 972
借酒劲吻你
借酒劲吻你 2020-12-12 23:26

I have my json_file.json like this:

[
{
    \"project\": \"project_1\",
    \"coord1\": 2,
    \"coord2\": 10,
    \"status\": \"yes\",
    \"priority\": 7
}         


        
3条回答
  •  庸人自扰
    2020-12-13 00:17

    The mongoimport tool has an option:

    --jsonArray treat input source as a JSON array

    Or it is possible to import from file containing same data format as the result of db.collection.find() command. Here is example from university.mongodb.com courseware some content from grades.json:

    { "_id" : { "$oid" : "50906d7fa3c412bb040eb577" }, "student_id" : 0, "type" : "exam", "score" : 54.6535436362647 }
    { "_id" : { "$oid" : "50906d7fa3c412bb040eb578" }, "student_id" : 0, "type" : "quiz", "score" : 31.95004496742112 }
    { "_id" : { "$oid" : "50906d7fa3c412bb040eb579" }, "student_id" : 0,       "type" : "homework", "score" : 14.8504576811645 }
    

    As you can see, no array used and no comma delimiters between documents either.

    I discover, recently, that this complies with the JSON Lines text format.

    Like one used in apache.spark.sql.DataFrameReader.json() method.

提交回复
热议问题