如何使用Java驱动程序在mongodb中禁止显示列? [英] How to suppress a column in mongodb using Java drivers?

查看:81
本文介绍了如何使用Java驱动程序在mongodb中禁止显示列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用Java实现以下mongo查询.

I would like to achieve the following mongo query in Java.

db.getCollection('document').find({ "$and": [
{"storeId":"1234"},
{"tranDate" : {"$gte":new Date("Sat,01 Oct 2016 00:00:00 GMT"),
               "$lte":new Date("Mon,31 Oct 2016 00:00:00 GMT")}}
]
},
{"_id" : 0})

我有以下Java代码,但是我不确定如何添加抑制逻辑,

I have the following java code, but I'm not sure how to add the suppress logic,

List<Bson> conditions = new ArrayList<>();
conditions.add(eq("field1", "value1"));
conditions.add(eq("field2", "value2"));
Bson query = and(conditions);
FindIterable<Document> resultSet = db.getCollection("document").find(query);

我需要在代码逻辑中添加{"_id":0}以禁止显示"_id"字段.请让我知道如何实现这一目标.

I need to add {"_id" : 0} in the code logic to suppress "_id" field. Kindly let me know how can I achieve this.

推荐答案

您可以尝试类似的方法.

You can try something like this.

import static com.mongodb.client.model.Projections.excludeId;

FindIterable<Document> resultSet = db.getCollection("document").find(query).projection(excludeId());

排除其他字段

import static com.mongodb.client.model.Projections.fields;

FindIterable<Document> resultSet = db.getCollection("document").find(query).projection(
fields(exclude("fieldname", "fieldvalue")));

有关投影的完整列表.

For complete list of projections.

http://api.mongodb. com/java/3.0/?com/mongodb/client/model/Projections.html http://mongodb.github.io/mongo-java-driver /3.0/builders/projections/

这篇关于如何使用Java驱动程序在mongodb中禁止显示列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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