数据存在时,Java MongoDB为find()返回null

数据存在时,Java MongoDB为find()返回null,java,mongodb,centos,Java,Mongodb,Centos,mongodb日志: MongoClient client = new MongoClient(new MongoClientURI("mongodb://127.0.0.1:27017")); > > 我的服务器日志: Document result = collection.find().first(); LOG.info(String.format("result of first %s", result)); 但当我通过mongo控制台访问时,数据存在 [02:54:28] [Ser

mongodb日志:

MongoClient client = new MongoClient(new MongoClientURI("mongodb://127.0.0.1:27017"));
>

>

我的服务器日志:

Document result = collection.find().first();
LOG.info(String.format("result of first %s", result));
但当我通过mongo控制台访问时,数据存在

[02:54:28] [Server thread/INFO]: result of first null
我真的不知道为什么会这样。。。
My Java MongoVersion:3.11.0

您在Java和Mongoshell中使用不同的集合。在Java代码中,您使用的是“raizant”数据库和“users”集合。在shell中,您正在使用
db.collection.find()进行测试;它应该是
db.users.find()
。否则,你的代码看起来没问题。@TimBiegeleisen没有。@prasad_uu你说得对,我错过了。我删除了我的答案。@TimBiegeleisen您发布的代码是准确的。没问题。@prasad_uu你说得对,我的收藏错了。。。collection.find().first()给我
文档{{{{{u id=5e018c3b3678df0eb9b7412b,username=Elric,password=the_user\u password}}
[02:54:28] [Server thread/INFO]: Opened connection [connectionId{localValue:2, serverValue:2}] to 127.0.0.1:27017
Document result = collection.find().first();
LOG.info(String.format("result of first %s", result));
[02:54:28] [Server thread/INFO]: result of first null
> use raizant
switched to db raizant
> db.getCollection('users')
raizant.users
> db.collection.find()
{ "_id" : ObjectId("5e016c9ed023a22f245ee8f1"), "username" : "Elric", "password" : "the_user_pass" }
{ "_id" : ObjectId("5e016caad023a22f245ee8f2"), "username" : "Knautiluz", "password" : "the_user_pass" }
> db.version() 
4.2.2