Get the single value from mongodb Collection using java code -


i want single value mongodb collection.

currently getting document finditerable.

customobject obj =  db         .getcollection("collection",customobject.class)         .find(and(eq("field1", new bigdecimal(10409)),eq("field2", new bigdecimal(1)))); 

but , dont want result in object or list.like in oracle use query single object :

select name employee_table id=10 , dept_id=23;

this query gives single name of employee on basis of filter conditions, , output in string type object.

same want mongodb , don't want use bean populate data. want single string object result.

you may use find method on collection, passing query , fields retrieve:

basicdbobject fields = new basicdbobject(); fields.put("name", true);  basicdbobject query = new basicdbobject(); query.put("id", 13); query.put("dept_id", 23);  dbcursor find = mongotemplate.getcollection("example").find(query, select); list<dbobject> list = find.toarray(); list<string> names = list.stream().map(o -> string.valueof(o.get("name"))).collect(collectors.tolist()); 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

unity3d - Fatal error- Monodevelop-Unity failed to start -