从mongodb结果java中删除_id [英] remove _id from mongodb result java

查看:770
本文介绍了从mongodb结果java中删除_id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是

  DBCollection collection = db.getCollection("volume");
  DBCursor cursor = collection.find();
  DBObject resultElement = cursor.next();
  Map resultElementMap = resultElement.toMap();
  System.out.println(resultElementMap);

结果是:

{_ id = 521b509d20954a0aff8d9b02,title = {"text":工作量 订单","x":-20.0},xAxis = {标题":{文本":"2012"}, 类别":["Jan","Feb","Mar","Apr","May","Jun","Jul" ,"Aug","Sep","Oct","Nov","Dec"]},yAxis = {"min":1000.0, "max":7000.0,"title":{"text":"Volume(K)"},"plotLines":[{ "label":{"text":"Average","x":25.0},"color":"black", "width":2.0,"value":30.0,"dashStyle":"solid"}]},图例= { "backgroundColor":"#FFFFFF","reversed":true},series = [{"name" :卷","showInLegend":假,数据":[2909.0,3080.0, 4851.0,3087.0,2960.0,2911.0,1900.0,3066.0,3029.0,5207.0,3056.0,3057.0]}]}

{_id=521b509d20954a0aff8d9b02, title={ "text" : "Volume Of Work Orders" , "x" : -20.0}, xAxis={ "title" : { "text" : "2012 "} , "categories" : [ "Jan" , "Feb" , "Mar" , "Apr" , "May" , "Jun" , "Jul" , "Aug" , "Sep" , "Oct" , "Nov" , "Dec"]}, yAxis={ "min" : 1000.0 , "max" : 7000.0 , "title" : { "text" : "Volume(K)"} , "plotLines" : [ { "label" : { "text" : "Average" , "x" : 25.0} , "color" : "black" , "width" : 2.0 , "value" : 30.0 , "dashStyle" : "solid"}]}, legend={ "backgroundColor" : "#FFFFFF" , "reversed" : true}, series=[ { "name" : "Volume" , "showInLegend" : false , "data" : [ 2909.0 , 3080.0 , 4851.0 , 3087.0 , 2960.0 , 2911.0 , 1900.0 , 3066.0 , 3029.0 , 5207.0 , 3056.0 , 3057.0]}]}

我需要从结果中删除_id.我了解我需要试用collection.find(),但请任何人可以帮助我吗?无法获得满意的结果

I need to remove _id from the result. I understand i need to play around with collection.find(), but please can anyone help me? Am not able to get sesired result

推荐答案

两个选项:

您可以从创建的地图中删除"_id"字段:

You can remove the "_id" field from the map created:

...
resultElementMap.remove("_id");
System.out.println(resultElementMap);

或者您可以要求查询结果不包含_id字段:

Or you can ask the query results to not include the _id field:

DBObject allQuery = new BasicDBObject();
DBObject removeIdProjection = new basicDBObject("_id", 0);

DBCollection collection = db.getCollection("volume");
DBCursor cursor = collection.find(allQuery, removeIdProjection);
DBObject resultElement = cursor.next();
Map resultElementMap = resultElement.toMap();
System.out.println(resultElementMap);

请参阅投影上的文档详细信息.

See the documentation on projections for all of the details.

这篇关于从mongodb结果java中删除_id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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