Search code examples
javamongodbmongo-java

In mongo shell can't find collection elements, but they were saved and loaded via Java driver


Start server:

bin\mongod.exe -dbpath=D:\mongo\dbs -rest  -logpath=logs\server.log -logappend -noauth

In java:

Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("test_db");
DBCollection collection = db.getCollection("Node");

BasicDBObject doc = new BasicDBObject();
doc.put("name", "test_name");
doc.put("type", "test_type");
collection.insert(doc);

DBCursor foundDocs = collection.find();
while (foundDocs.hasNext()) {
 System.out.println(foundDocs.next());
}
mongo.close();

have this:

 { "_id" : { "$oid" : "4f2a8e3976cd9f84b75cce75"} , "name" : "test_name" , "type" : "test_type"}

After, I can't see any elements:

> use test_db
switched to db test_db
> db.test_db.count()
0
> db.test_db.find()
> db.test_db.Node.count()
0
>

why?


Solution

  • AFAIK you should not repeat the name of the db:

    db.Node.count();