使用java代码从mongodb集合中获取单个值 [英] Get the single value from mongodb Collection using java code

查看:30
本文介绍了使用java代码从mongodb集合中获取单个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想从 Mongodb 集合中获取单个值.

I want to get only the single value from Mongodb Collection.

目前我正在获取 FindIterable 的文档.

Currently i am getting the Document which is FindIterable.

CustomObject obj =  db
        .getCollection("Collection",CustomObject.class)
        .find(and(eq("field1", new BigDecimal(10409)),eq("field2", new BigDecimal(1))));

但是,我不希望在对象或列表中有任何结果.就像在 Oracle 中,我们使用查询来获取单个对象:

But , i dont want any result in to the Object or List.like in Oracle we use Query to get single Object :

SELECT NAME FROM EMPLOYEE_TABLE WHERE ID=10 AND DEPT_ID=23;

此查询根据过滤条件为我们提供了员工的 Single 名称,我们得到了 String 类型对象的输出.

This query gives us the Single name of the employee on the basis of filter conditions, and we get the output in String type Object.

同样我想从 mongodb 获取,我不想使用任何 bean 来填充数据.我只想要单个 String 对象作为结果.

Same i want to get from the mongodb , i don't want to use any bean to populate the data. i want only single String object as result.

推荐答案

如果你正在寻找一个只返回一个文档的函数,那么你可以使用 findOne.

If you are seeking for a function that returns just one document, then you can use findOne.

这篇关于使用java代码从mongodb集合中获取单个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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