Mongodb Java-如何使用find()或findOne()返回受限字段 [英] Mongodb Java - How to return restricted fields with find() or findOne()

查看:61
本文介绍了Mongodb Java-如何使用find()或findOne()返回受限字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用驱动程序Java Mongodb,我正在寻找一种方法来返回仅受限制的字段,并带有 find()findOne(). 例如,我有一个"people"集合,其中包含以下字段:"id""name""surname""address""city" ...,而我只想返回"name""surname"

我在网上搜索了一下,发现了这个代码示例Java Mongodb:解决方案

您可以传递带有字段名称的另一个DBObject并将其传递到此处:

cur = coll.find(new BasicDBObject("id", 6655), your_dbobject_with_field_names);

这是 with fields : "id", "name", "surname", "address", "city"... and I just want to return "name" and "surname"

I searched on the Web and I just found this example of code Java Mongodb : http://vsbabu.org/mt/archives/2010/03/02/simple_mongodbjava_example.html

解决方案

You can pass another DBObject with the names of the fields and pass it here:

cur = coll.find(new BasicDBObject("id", 6655), your_dbobject_with_field_names);

Here is the API documentation

这篇关于Mongodb Java-如何使用find()或findOne()返回受限字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆