限制Result中的字段 [英] Restrict fields in Result

查看:80
本文介绍了限制Result中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MongoDB v3.0.1和MongoDB Java驱动程序3.0.0-RC1。

I am working with MongoDB v3.0.1 and the MongoDB Java Driver 3.0.0-RC1.

我有一个用户集合,其中包含username, firstname,lastname,email等等。

I have an user collection with fields like "username", "firstname", "lastname", "email", and so on.

现在我想选择所有用户,但只选择字段username,firstname和lastname。

Now I want to select all users but only with the fields "username", "firstname" and "lastname".

在Mongo-Shell上它正在使用 db.user.find({},{username:true, firstname:true,lastname:true})

On the Mongo-Shell it is working with db.user.find({}, { "username" : true , "firstname" : true , "lastname" : true})

但是我怎么能用Java做呢?我尝试使用
final BasicDBObject query = new BasicDBObject({},new BasicDBObject(_ id,true));
最终MongoCursor<文件> usersCursor = col.find(query)

But how can I do it in Java? I tried it with final BasicDBObject query = new BasicDBObject("{}", new BasicDBObject("_id", true)); final MongoCursor<Document> usersCursor = col.find(query)

为此,我得到一个空结果,因为它被翻译为 { {}:{_ id:true,firstname:true,lastname:true}}

For this I get an empty result back because it's translated as { "{}" : { "_id" : true , "firstname" : true , "lastname" : true}}.

我也尝试过它与BasicDBList,但 col.find()

I also tried it with BasicDBList, but this isn't accepted by col.find()

不接受这个旧Mongo 2.x驱动程序我会使用 new BasicDBObject(BasicDBObject(),new BasicDBObject(username,true).append(firstname,true).append(lastname,true)

With the "old" Mongo 2.x driver I would use new BasicDBObject(BasicDBObject(), new BasicDBObject("username", true).append("firstname", true).append("lastname", true)

是否有可能这样做或者我是否需要获取所有字段?

Is there a possibility to do that or do I have to fetch all fields?

问候
$ b $bSören

Greetings
Sören

推荐答案

使用3.0.0 Java驱动程序中的新CRUD API,正确的方法使用MongoCollection.find()链接的投影方法。由于投影方法采用Bson接口的实例,因此可以使用许多不同的类来指定投影:

With the new CRUD API in the 3.0.0 Java driver, the proper way to do it is with the projection method that is chained off off MongoCollection.find(). Since the projection method takes an instance of the Bson interface, there are a number of different classes you can use to specify the projection:

    // using BasicDBObject
    collection.find().projection(new BasicDBObject("username", true)
                                 .append("lastname", true)
                                 .append("firstname", true))

    // using the new Document class
    collection.find().projection(new Document("username", true)
                                 .append("lastname", true)
                                 .append("firstname", true));

    // Using the new Projections builder
    collection.find().projection(Projections.include("username", "lastname", "firstname"));

至于你说它在2.x驱动程序中的工作方式,这是不可能的,因为

As for the way you say it works in the 2.x driver, that's not possible, as

new BasicDBObject(BasicDBObject(), BasicDBObject("username", true)
                                  .append("firstname", true)
                                  .append("lastname", true)

无法编译。我是不知道你究竟用2.x做了什么,但是使用2.x中的DBCollection类(在3.0驱动程序中仍然支持)实现此目的的正确方法是:

does not compile. I'm not sure what exactly you were doing with 2.x, but the proper way to accomplish this with the DBCollection class in 2.x (which is still supported in the 3.0 driver), is:

    collection.find(new BasicDBObject(), new BasicDBObject("username", true)
                                        .append("lastname", true)
                                        .append("firstname", true));

这篇关于限制Result中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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