簡體   English   中英

MongoDB shell(mongo)無法正常工作

[英]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('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

改變我的願望db:

> 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命令指向同一個db)。

這是我的版本:

$ mongo --version
MongoDB shell version v3.6.1

$ mongod --version
db version v3.0.15

$ node --version
v8.9.1

我在這里錯過了什么? 我的shell命令有問題嗎?

如果缺少相關信息,請告知我們。

您必須使用要執行請求的collection的名稱替換collection .Plus更改集合的名稱,以這種方式請求它(使用 - separator)將無效。

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

看看mongodb文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM