【问题标题】:MongoDB shell (mongo) not workingMongoDB shell(mongo)不工作
【发布时间】:2018-06-15 09:47:13
【问题描述】:

我有 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 方法,显示文档(这就是我知道插入工作正常的方式):

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 shell 中,我可以看到我的数据库 (mytestingdb):

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

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

> use mytestingdb
switched to db 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 不再显示文档(所以我确定 insert、find 和 shell 命令指向同一个数据库)。

这是我的版本:

$ mongo --version
MongoDB shell version v3.6.1

$ mongod --version
db version v3.0.15

$ node --version
v8.9.1

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

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

【问题讨论】:

标签: node.js mongodb


【解决方案1】:

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

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

mongodb documentation

【讨论】:

  • 谢谢@Grégory NEUT,我已经尝试过了,它给了我这个错误:2018-01-05T14:58:04.552+0000 E QUERY [thread1] ReferenceError: course is not defined : @(shell):1:1
  • 好的,这意味着您不能将- 放在集合名称中。 Look here
  • 真的吗??这太合乎逻辑了!!让我试试,我会告诉你它是怎么回事。
  • 你是对的,问题是集合名称中的-,我已将'edx-course-students' 替换为'students',现在db.students.find({}); 显示所有文档。非常感谢先生。
猜你喜欢
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 2020-01-07
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多