2013-08-27 49 views
3

我的代码是删除_id

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

,其结果是:

{_id = 521b509d20954a0aff8d9b02,标题= { “文”: “卷工作 订单”, “x”:-20.0} xAxis = {“title”:{“text”:“2012”}, “categories”:[“Jan”,“Feb”,“Mar”,“Apr” ,“Jun”,“Jul” ,“Aug”,“Sep”,“Oct”,“Nov”,“Dec”]},yAxis = {“min”:1000.0, “max”:7000.0“title “:{”text“:”Volume(K)“}, “plotLines”:[{“{标签”:{“text”:“平均”,“x”:25.0},“颜色”:“黑色”, “width”:2.0,“value”:30.0,“dashStyle “:”solid“}]},legend = {”backgroundColor“:”#FFFFFF“,”reversed“:true},series = [{”name“ :”Volume“,”showInLegend“:false, “:[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(),但请任何人都可以帮助我?我没能获得sesired结果

回答

5

两个选项:

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

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

或者,你可以要求查询结果不包括_id字段:

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); 

有关所有详细信息,请参阅projections的相关文档。

+0

真棒..作品像魅力..谢谢很多兄弟... – user2572739