MongoDB shell (mongo) not working

22 浏览
0 Comments

MongoDB shell (mongo) not working

我使用以下命令运行了mongod

$ mongod -f /etc/mongodb.conf 

使用以下脚本成功插入了文档:

insert.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('连接成功');
  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('插入错误');
      return process.exit(1);
    }
    console.log('result.result.n :' + result.result.n);
    console.log('result.ops.length: ' + result.ops.length);
    console.log('成功插入了3个文档到edx-course-students集合中');
  });
  client.close();
})

以下是输出结果:

$ node insert.js                                                          
连接成功
result.result.n :3
result.ops.length: 3
成功插入了3个文档到edx-course-students集合中

而且,当我运行find方法时,文档显示出来(这是我知道插入成功的原因):

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('连接成功');
  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(`找到以下文档:`);
    console.dir(docs);
  });
  client.close();
});

以下是输出结果:

$ node find.js 
连接成功
docs.length: 3
找到以下文档:
[ { _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 shell中,我可以看到我的数据库(mytestingdb):

> show dbs

blog 0.031GB

local 0.078GB

mytestingdb 0.031GB

test 0.078GB

并且切换到我想要的数据库:

> use mytestingdb
切换到数据库mytestingdb

我可以看到我的集合(edx-course-students):

> show collections

edx-course-students

system.indexes

但是find()没有显示任何文档,count()返回0:

> db.collection.find({});

> db.collection.count();

0

注意:如果我使用dropDatabase()删除mytestingdb,像这样:

> db.dropDatabase();

{ "dropped" : "mytestingdb", "ok" : 1 }

find.js将不再显示这些文档(所以我确信插入、查找和shell命令指向同一个数据库)。

以下是我的版本:

$ mongo --version
MongoDB shell version v3.6.1
$ mongod --version
db version v3.0.15
$ node --version
v8.9.1

我在这里缺少什么?我的shell命令有问题吗?

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

0
0 Comments

MongoDB shell (mongo) not working的问题出现的原因是集合名称中含有连字符“-”,导致无法正确执行查询操作。解决方法是将集合名称中的连字符替换为有效的字符,然后重新执行查询操作。

具体操作如下:

1. 将collection替换为要执行请求的集合的名称。

2. 修改集合的名称,使用连字符“-”作为分隔符是无效的。

3. 参考MongoDB官方文档(https://docs.mongodb.com/manual/reference/method/db.collection.find/)进行操作。

原始问题代码如下:

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

问题解决过程如下:

1. 进行了上述修改后,仍然出现了错误。

2. 错误信息如下:2018-01-05T14:58:04.552+0000 E QUERY [thread1] ReferenceError: course is not defined : @(shell):1:1

3. 通过查看相关文档(https://stackoverflow.com/questions/9868323),发现集合名称中不应该包含连字符“-”。

4. 进行修改,将'edx-course-students'替换为'students'

5. 重新执行查询操作:db.students.find({});

6. 现在可以正确显示所有文档了。

感谢égory NEUT的帮助和提示。

0