You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MongoDB未插入全部文档:插入8条仅显示5条的问题

MongoDB插入8个文档仅显示5个的问题

我尝试向MongoDB的mycollection集合插入8个文档,插入操作的确认消息显示已成功插入8个文档,但执行db.mycollection.countDocuments()仅返回5,使用MongoDB Compass GUI查看也得到同样结果。

插入代码

db.mycollection.insertMany(
   [  {
    "_id": "e89c4c7a-bfdc-4433-bf68-2b3ea4deaad6",
    "name": "Dunn England",
    "isActive": false,
    "balance": "1,505.70",
    "picture": "http://placehold.it/32x32",
    "age": 25,
    "eyeColor": "brown",
    "gender": "male"
  },
  {
    "_id": "4b5cc8e8-6683-4dd9-a0c3-dd683c41c32f",
    "name": "Elizabeth Cunning",
    "isActive": false,
    "balance": "505.70",
    "picture": "http://placehold.it/32x32",
    "age": 19,
    "eyeColor": "brown",
    "gender": "female"
  },
  {
    "_id": "808259cb-0ea3-4424-b626-c147655625fc",
    "name": "Gabrielle Dane",
    "isActive": true,
    "balance": "5505.70",
    "picture": "http://placehold.da/32x32",
    "age": 38,
    "eyeColor": "blue",
    "gender": "female"
  },
  {
    "_id": "26a0278a-d825-42f9-a697-47a53f69e0c5",
    "name": "Renee Morrow",
    "isActive": true,
    "balance": "2,022.81",
    "picture": "http://placehold.it/32x32",
    "age": 24,
    "eyeColor": "green",
    "gender": "female"
  },
  {
    "_id": "d631ff57-c77c-4447-bad9-37b2147ed18f",
    "name": "Jake Roberts",
    "isActive": false,
    "balance": "6022.81",
    "picture": "http://placehold.it/32x32",
    "age": 24,
    "eyeColor": "green",
    "gender": "male"
  },
  {
    "_id": "48b7a108-07c9-4107-aed8-637756caa430",
    "name": "Urmina Roberts",
    "isActive": true,
    "balance": "7,6022.81",
    "picture": "http://placehold.it/32x32",
    "age": 24,
    "eyeColor": "green",
    "gender": "female"
  },
  {
    "_id": "3a75d9dc-ef66-452b-9e12-9dcda2a33977",
    "name": "Emily Waters",
    "isActive": false,
    "balance": "76022.81",
    "picture": "http://placehold.ww/34x34",
    "age": 44,
    "eyeColor": "brown",
    "gender": "female"
  },
  {
    "_id": "b01a9244-28b5-4c89-90d5-70fb1ec573e2",
    "name": "Scott Turner",
    "isActive": true,
    "balance": "1,894.19",
    "picture": "http://placehold.dk/32x32",
    "age": 88,
    "eyeColor": "green",
    "gender": "male"
  }]
)

插入确认与计数结果

acknowledged: true,
  insertedIds: {
    '0': 'e89c4c7a-bfdc-4433-bf68-2b3ea4deaad6',
    '1': '4b5cc8e8-6683-4dd9-a0c3-dd683c41c32f',
    '2': '808259cb-0ea3-4424-b626-c147655625fc',
    '3': '26a0278a-d825-42f9-a697-47a53f69e0c5',
    '4': 'd631ff57-c77c-4447-bad9-37b2147ed18f',
    '5': '48b7a108-07c9-4107-aed8-637756caa430',
    '6': '3a75d9dc-ef66-452b-9e12-9dcda2a33977',
    '7': 'b01a9244-28b5-4c89-90d5-70fb1ec573e2'
}

local> db.mycollection.countDocuments()
5

问题原因与解决方法

核心原因

你执行计数命令时的命令行前缀是local>,说明当前操作的是local数据库,但插入文档时大概率是在其他数据库(比如默认的test数据库)下执行的。MongoDB的集合属于特定数据库,跨数据库查询自然无法看到目标数据。

解决步骤

  1. 切换回插入文档时使用的数据库(以默认的test为例):
use test
  1. 再次执行计数命令:
db.mycollection.countDocuments()
  1. 规范操作:后续插入或查询时,先明确切换到目标数据库,避免跨库操作混淆。

额外检查

在MongoDB Compass中,确认当前连接的数据库与插入文档时的数据库一致,重新查询集合即可看到全部8个文档。

内容的提问来源于stack exchange,提问作者codingjoe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 08:06:04