Mongo java驱动程序 - 只获取数据而不是字段名称

时间:2013-12-10 17:51:21

标签: java mongodb

如何让我的查询只返回它拥有的数据,而不是字段,

例如,这是我的代码

Pattern pattern = Pattern.compile(".*"+textArea.getText()+".*", Pattern.CASE_INSENSITIVE);
BasicDBObject query = new BasicDBObject("content:encoded", pattern);

   model.setRowCount(0);
      BasicDBObject field = new BasicDBObject();
      field.put("title", 1); 
      field.put("_id", 0); 
      DBCursor cursor = blogTable.find(query,field);

      String[] columnNames = {"Title"};               //Jtable headings

     while (cursor.hasNext())
               {
                model.addRow(new Object[]{cursor.next().toString()});
               }

,我得到的输出是

{ "title" : "Friday - 9/24/2012"}
{ "title" : "Picture from Pile Gate in Dubrovnik"}
{ "title" : "Saturday - 8/25/2012 - In search of the coffee press"}
{ "title" : "Concert at Church of St. Blaise"}
{ "title" : "DSCN0040"}
{ "title" : "DSCN0041"}
{ "title" : "DSCN0043"}
{ "title" : "DSCN0042"}

我希望它只有数据部分,即

Friday - 9/24/2012
Picture from Pile Gate in Dubrovnik
Saturday - 8/25/2012 - In search of the coffee press
Concert at Church of St. Blaise
DSCN0040

提前致谢。

1 个答案:

答案 0 :(得分:1)

Mongodb以JSON格式返回Document而不是特定字段。

试试此代码

 while (cursor.hasNext()){
            model.addRow(new Object[]{cursor.next().get("title")});
 }