如何将 json 文件内容作为文档导入 mongodb



Users.json 文件:

{
  "id": 1,
  "name": "abc"
},
{
  "id": 2,
  "name": "pqr"
}

如何在MongoDB中插入包含50K文档的JSON数据(作为文档而不是数组(

你可以使用 mongo-shell mongoimport 和下面给出的命令

mongoimport --db databaseName --collection collectionName --drop --file ~/path/to/file/fileName.json

您可以复制整个 json,然后使用 insert(( 在相应的集合中插入一个文档。

例如:集合名称:用户

db.getCollection('users').insert(
{
    "Users": [{
          "id": 1,
           "name": "abc"
      },
      {
        "id": 2,
         "name": "pqr"
     )]
 })

为了添加多个文档,请使用insertMany((

例如:

db.getCollection('users').insertMany(
    [{
        "Users": [{
              "id": 1,
               "name": "abc"
          },
          {
            "id": 2,
             "name": "pqr"
         )]
     },
     {
        "Users": [{
              "id": 3,
               "name": "abc"
          },
          {
            "id": 4,
             "name": "pqr"
         )]
     }]
)
db.collection.insert(
   <document or array of documents>,
   {
     writeConcern: <document>,
     ordered: <boolean>
   }
)

最新更新