Importing json from file into mongodb using mongoimport

后端 未结 3 961
借酒劲吻你
借酒劲吻你 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:05

    I faced opposite problem today, my conclusion would be:

    If you wish to insert array of JSON objects at once, where each array entry shall be treated as separate dtabase entry, you have two options of syntax:

    1. Array of object with valid coma positions & --jsonArray flag obligatory

      [
        {obj1},
        {obj2},
        {obj3}
      ]
      
    2. Use file with basically incorrect JSON formatting (i.e. missing , between JSON object instances & without --jsonArray flag

      {obj1}
      {obj2}
      {obj3}
      

    If you wish to insert only an array (i.e. array as top-level citizen of your database) I think it's not possible and not valid, because mongoDB by definition supports documents as top-level objects which are mapped to JSON objects afterwards. In other words, you must wrap your array into JSON object as ALAN WARD pointed out.

提交回复
热议问题