Reputation: 2654
I have mongod
running with the following command:
$ mongod -f /etc/mongodb.conf
The documents were inserted OK with this script:
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();
})
This is the output:
$ node insert.js
Connection is OK
result.result.n :3
result.ops.length: 3
Inserted 3 documents into the edx-course-students collection
And I run the find
method, the documents are shown (that is how I know the insert worked fine):
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();
});
This is the output:
$ 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' } ]
The problem is that I can't see the documents in the MongoDB shell, I've tried this:
Connecting to MongoDB shell with:
$ mongo
Inside MongoDB shell I can see my database (mytestingdb):
> show dbs
blog 0.031GB
local 0.078GB
mytestingdb 0.031GB
test 0.078GB
And change to my desire db:
> use mytestingdb
switched to db mytestingdb
I can see my collection (edx-course-students):
> show collections
edx-course-students
system.indexes
But no documents are shown with find()
, and count()
returns 0:
> db.collection.find({});
> db.collection.count();
0
Note: If I delete mytestingdb with dropDatabase()
, like this:
> db.dropDatabase();
{ "dropped" : "mytestingdb", "ok" : 1 }
the find.js no longer shows the documents (so I'm sure the insert, find and the shell commands are pointing to the same db).
This are my versions:
$ mongo --version
MongoDB shell version v3.6.1
$ mongod --version
db version v3.0.15
$ node --version
v8.9.1
What am I missing here? There is something wrong with my shell commands?
If any relevant information is missing, please let me know.
Upvotes: 4
Views: 8422
Reputation: 23495
You gotta replace the collection
by the name of the collection you want to perform the request on.Plus change the name of your collection, it would be invalid to request it that way (with - separators).
db.edx.course.students.find({});
Look at mongodb documentation
Upvotes: 1