【问题标题】:Why i am getting the total count as ZERO even after importing 240 documents succesfully?为什么即使在成功导入 240 个文档后总计数为零?
【发布时间】:2021-11-20 04:15:06
【问题描述】:

实际上我正在学习 MongoDB,因为我被困在导入 JSON 文件(位于我笔记本电脑的桌面)

我的 JSON 文件是由 240 个元素组成的数组,每个元素的类型都相似,如下所示(我在数组中添加了两个这样的文档,类似地,其他 238 个具有相同形式的文档存在于同一数组中):-

[
  {
    "id": 1,
    "url": "http://www.tvmaze.com/shows/1/under-the-dome",
    "name": "Under the Dome",
    "type": "Scripted",
    "language": "English",
    "genres": [
      "Drama",
      "Science-Fiction",
      "Thriller"
    ],
    "status": "Ended",
    "runtime": 60,
    "premiered": "2013-06-24",
    "officialSite": "http://www.cbs.com/shows/under-the-dome/",
    "schedule": {
      "time": "22:00",
      "days": [
        "Thursday"
      ]
    },
    "rating": {
      "average": 6.5
    },
    "weight": 91,
    "network": {
      "id": 2,
      "name": "CBS",
      "country": {
        "name": "United States",
        "code": "US",
        "timezone": "America/New_York"
      }
    },
    "webChannel": null,
    "externals": {
      "tvrage": 25988,
      "thetvdb": 264492,
      "imdb": "tt1553656"
    },
    "image": {
      "medium": "http://static.tvmaze.com/uploads/images/medium_portrait/0/1.jpg",
      "original": "http://static.tvmaze.com/uploads/images/original_untouched/0/1.jpg"
    },
    "summary": "<p><b>Under the Dome</b> is the story of a small town that is suddenly and inexplicably sealed off from the rest of the world by an enormous transparent dome. The town's inhabitants must deal with surviving the post-apocalyptic conditions while searching for answers about the dome, where it came from and if and when it will go away.</p>",
    "updated": 1529612668,
    "_links": {
      "self": {
        "href": "http://api.tvmaze.com/shows/1"
      },
      "previousepisode": {
        "href": "http://api.tvmaze.com/episodes/185054"
      }
    }
  },
  {
    "id": 2,
    "url": "http://www.tvmaze.com/shows/2/person-of-interest",
    "name": "Person of Interest",
    "type": "Scripted",
    "language": "English",
    "genres": [
      "Drama",
      "Action",
      "Crime"
    ],
    "status": "Ended",
    "runtime": 60,
    "premiered": "2011-09-22",
    "officialSite": "http://www.cbs.com/shows/person_of_interest/",
    "schedule": {
      "time": "22:00",
      "days": [
        "Tuesday"
      ]
    },
    "rating": {
      "average": 9
    },
    "weight": 96,
    "network": {
      "id": 2,
      "name": "CBS",
      "country": {
        "name": "United States",
        "code": "US",
        "timezone": "America/New_York"
      }
    },
    "webChannel": null,
    "externals": {
      "tvrage": 28376,
      "thetvdb": 248742,
      "imdb": "tt1839578"
    },
    "image": {
      "medium": "http://static.tvmaze.com/uploads/images/medium_portrait/163/407679.jpg",
      "original": "http://static.tvmaze.com/uploads/images/original_untouched/163/407679.jpg"
    },
    "summary": "<p>You are being watched. The government has a secret system, a machine that spies on you every hour of every day. I know because I built it. I designed the Machine to detect acts of terror but it sees everything. Violent crimes involving ordinary people. People like you. Crimes the government considered \"irrelevant\". They wouldn't act so I decided I would. But I needed a partner. Someone with the skills to intervene. Hunted by the authorities, we work in secret. You'll never find us. But victim or perpetrator, if your number is up, we'll find you.</p>",
    "updated": 1535507028,
    "_links": {
      "self": {
        "href": "http://api.tvmaze.com/shows/2"
      },
      "previousepisode": {
        "href": "http://api.tvmaze.com/episodes/659372"
      }
    }
  }
]

我也尝试使用此命令:-
C:\Users\adity>mongoimport --jsonArray --db movie --collection movies --file C:\Users\adity\Desktop\tv-shows.json

我认为数据已经成功导入,因为我得到了这个

C:\Users\adity>mongoimport --jsonArray --db movie --collection movies --file C:\Users\adity\Desktop\tv-shows.json
2021-09-29T12:43:37.247+0530    connected to: mongodb://localhost/
2021-09-29T12:43:37.288+0530    240 document(s) imported successfully. 0 document(s) failed to import.

但是当我运行以下命令时,它向我显示了一些非预期的输出:-

C:\WINDOWS\system32>net start mongoDB
The MongoDB Server (MongoDB) service is starting.
The MongoDB Server (MongoDB) service was started successfully.


C:\WINDOWS\system32>mongo
MongoDB shell version v5.0.3
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("590da209-aceb-4e56-960b-c950384ee3af") }
MongoDB server version: 5.0.3
================
Warning: the "mongo" shell has been superseded by "mongosh",
which delivers improved usability and compatibility.The "mongo" shell has been deprecated and will be removed in
an upcoming release.
We recommend you begin using "mongosh".
For installation instructions, see
https://docs.mongodb.com/mongodb-shell/install/
================
---
The server generated these startup warnings when booting:
        2021-09-29T13:39:59.232+05:30: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
---
---
        Enable MongoDB's free cloud-based monitoring service, which will then receive and display
        metrics about your deployment (disk utilization, CPU, operation statistics, etc).

        The monitoring data will be available on a MongoDB website with a unique URL accessible to you
        and anyone you share the URL with. MongoDB may use this information to make product
        improvements and to suggest MongoDB products and deployment options to you.

        To enable free monitoring, run the following command: db.enableFreeMonitoring()
        To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
WARNING: some history file lines were truncated at 4095 bytes.
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
movie   0.001GB
test    0.000GB
> use movie
switched to db movie
> db.movie.find().count()
0
> db.movie.find().pretty()
>

我使用的是 Windows 操作系统。

有人可以帮我,为什么我将 count 的值设为 ZERO??

【问题讨论】:

  • 您不能将 json 文件从桌面移动/复制到任何其他文件夹吗?见superuser.com/questions/1489874/…
  • 请编辑您的问题并将此类信息放在那里,而不是添加评论。你的 JSON 文件是什么样子的?请不要提供截图,使用格式化文本。
  • 您的示例显示了一个 JSON 元素。元素是如何分离的? --jsonArray 选项需要 JSON 文件,例如 [{"id": ...}, {...}]
  • 很抱歉在这个平台上提问的新手,这就是为什么以前理解我的问题时有很多问题现在我已经尽可能多地编辑了这个问题

标签: database mongodb sqlite mongodb-query mongodb-.net-driver


【解决方案1】:

尝试使用 Studio 3T(https://studio3t.com/download/)。 GUI 支持导入/导出 JSON/CSV 等。

【讨论】:

  • 你想做什么?哪个操作系统?
  • 我想使用 CMD(以管理员身份运行)选项导入位于我的桌面屏幕 (WINDOWS) 上的 JSON 文件。
  • 在桌面打开CMD并运行mongoimport --db=yourdbname --collection=yourcollectionname --file=yourfilename.json
  • 您使用的是 JSON 数组还是单独的 JSON 对象行(文本)?可以在这里分享一下格式吗?
  • @Adityakumar mongoimport --jsonArray --db movie --collection movies --file C:\Users\adity\Desktop 在这一行中,您没有传递文件名。
【解决方案2】:

文件名丢失。

用途:

mongoimport --jsonArray --db movie --collection movies --file C:\Users\adity\Desktop\yourfilename.json

代替:

mongoimport --jsonArray --db movie --collection movies --file C:\Users\adity\Desktop\.

或者在你的 CMD 中 cd Desktop 并提供:

mongoimport --jsonArray --db movie --collection movies --file yourfilename.json

【讨论】:

  • @Adityakumar 您应该首先选择数据库。例如:在show dbs 之后输入use movie 并输入。这将进入电影数据库,然后输入show collections 它会给你集合名称。然后输入db.yourcollectionname.find()
  • @Adityakumar 你的收藏名称是movies。所以请使用db.movies.find()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
  • 2018-05-24
  • 1970-01-01
  • 2018-09-26
  • 1970-01-01
  • 2016-04-09
  • 2021-03-05
相关资源
最近更新 更多