当前位置: 首页 > 知识库问答 >
问题:

MongoDB shell(mongo)不工作

缪远航
2023-03-14

我有mongod运行以下命令:

$ mongod -f /etc/mongodb.conf 

使用以下脚本可以插入文档:

插入. js

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/edx-course-db';

MongoClient.connect(url, (error, client) => {
  if (error) return process.exit(1);

  console.log('Connection is OK');
  var db = client.db('mytestingdb');  
  var collection = db.collection('edx-course-students');

  collection.insert([
    {name : 'Bob'}, {name : 'John'}, {name : 'Peter'}
  ], (error, result) => {
    if (error) { 
      console.log('error in insert');
      return process.exit(1);
    }
    console.log('result.result.n :' + result.result.n);
    console.log('result.ops.length: ' + result.ops.length);
    console.log('Inserted 3 documents into the edx-course-students collection');
  });
  client.close();
})

这是输出:

$ node insert.js                                                          
Connection is OK
result.result.n :3
result.ops.length: 3
Inserted 3 documents into the edx-course-students collection

并且我运行查找方法,显示了文档(这就是我知道插入工作正常的原因):

find.js

const MongoClient = require('mongodb').MongoClient;
const url = 'mongodb://localhost:27017/edx-course-db';

MongoClient.connect(url, (error, client) => {
  if (error) return process.exit(1);

  console.log('Connection is OK');
  var db = client.db('mytestingdb');  
  var collection = db.collection('edx-course-students');

  collection.find({}).toArray((error, docs) => {
    if (error) return process.exit(1);
    console.log(`docs.length: ${docs.length}`);
    console.log(`Found the following documents:`);
    console.dir(docs);
  });
  client.close();
});

这是输出:

$ node find.js 
Connection is OK
docs.length: 3
Found the following documents:
[ { _id: ObjectID { _bsontype: 'ObjectID', id: [Object] },
    name: 'Bob' },
  { _id: ObjectID { _bsontype: 'ObjectID', id: [Object] },
    name: 'John' },
  { _id: ObjectID { _bsontype: 'ObjectID', id: [Object] },
    name: 'Peter' } ]

问题是我在MongoDB shell中看不到文档,我试过这个:

通过以下方式连接到MongoDB shell:

$ mongo

在MongoDB外壳中,我可以看到我的数据库(mytestingdb):

> show dbs
blog         0.031GB
local        0.078GB
mytestingdb  0.031GB
test         0.078GB

并更改为我想要的数据库:

> use mytestingdb
switched to db mytestingdb

我可以看到我的收藏(edx课程学生):

> show collections
edx-course-students
system.indexes

但是没有显示带有< code>find()的文档,并且< code>count()返回0:

> db.collection.find({});
> db.collection.count();
0

注意:如果我用dropDatabase()删除mytestingdb,如下所示:

> db.dropDatabase();
{ "dropped" : "mytestingdb", "ok" : 1 }

发现。js不再显示文档(所以我确信insert、find和shell命令指向同一个数据库)。

这是我的版本:

$ mongo --version
MongoDB shell version v3.6.1

$ mongod --version
db version v3.0.15

$ node --version
v8.9.1

我在这里错过了什么?我的shell命令有问题吗?

如果缺少任何相关信息,请告诉我。

共有1个答案

谈禄
2023-03-14

您必须将集合替换为您要执行请求的集合的名称。另外,更改集合的名称,以这种方式请求它是无效的(使用-分隔符)。

db.edx.course.students.find({});

查看 mongodb 文档

 类似资料:
  • 我有这样的配置 在包中,有一个组件需要中的存储库。 这适用于spring数据MongoDB 1.5.2版 如果我升级到任何高于1.6.0.release的版本(我尝试过1.6.2和1.7.0),这将不再有效。这就好像mongo存储库扫描不工作,我得到了错误: 有什么想法吗? 更新:如果我使用MongoRepository而不是PagingAndSortingRepository,它可以工作: 这不

  • 我有一个问题让我忙了3天,但仍在与之斗争:我有一个mongodb查询,如下所示: 现在我正试图将其转换为java,但我真的不知道如何转换这一行: 我尝试了此代码,但根本不起作用: 但我得到了以下错误: 通用域名格式。mongodb。CommandFailureException: {“serverUsed”:“…”,“errmsg”:“异常:无效 运算符'$dateToString'”,“代码”:

  • 我试图在MongoDb上获取一个名为“fecha”的日志,其中包含一个日期字段。 在Java中执行代码后,我得到下一个查询: 但是我没有得到任何结果。以下是一些数据样本: 我的Java代码是下一个: 谢谢

  • 我们的Camel Mongo DB组件不支持多个Mongo DB连接。默认情况下,它在所有mongoendpoint中使用单个Mongodb bean 问题: 我为两个数据库创建了两个不同的mongo bean实例,并分别进行了身份验证。在Spring创建了两个MongoDBeans实例 使用DB初始化/创建mongoendpoint(具有不同DB的多个endpoint)时,它将第一个mongoe

  • MongoDB README Welcome to MongoDB! Components mongod - The database server. mongos - Sharding router. mongo - The database shell (uses interactive javascript). Utilities install_compass - Installs Mon

  • 我正在使用Spring Mongo审计和@CreatedDate@CreatedBy不工作,但@LastModifiedDate和@LastModifiedBy工作正常。 我在配置类上添加了@EnableMongoAudting,并定义了AuditAware。 审核类别为: 当我保存文档时,它在createdOn和createdBy中都设置为null,但在modifiedOn和modifiedBy