MongoDB java驱动程序使用正则表达式查询来检索前两个字符

时间:2017-05-05 06:00:08

标签: java regex mongodb

我想检索前两个字符值,如果我给02-01-2017 00:05:46我应该得到像

的结果
02-01-2017 00:05:46 
02-01-2017 00:05:46 
02-01-2017 00:05:46 

没有字符串SyTime_000_09_00:00

我正在使用jongo library和mongo drive 3.4.2

{
  "_id" : ObjectId("590b70c609200535e85c6540"),
  "Data" : "02-01-2017 00:05:46 SyTime_000_09_00:00 "
}
{
  "_id" : ObjectId("590b70c609200535e85c6541"),
  "Data" : "02-01-2017 00:05:46 DPMPow_P01_04_  45.53 "
}

这是我试过的

public class DataSource {
  private MongoCollection collection;

  public void DataSource() {
    this.collection = (new Jongo(new
        MongoClient("localhost", 27017).getDB("admin"))).getCollection("test");
  }

  public void guarder(Object obj) {
    this.collection.save(obj);
  }

  public Iterable consol() {
    DBObject query = new BasicDBObject();

    Pattern regex = Pattern.compile("02-01-2017");
    query.put("Data", regex);
    return (Iterable) this.collection.find(query.toString()).as(pojo.class);
  }
}

这里是pojo类

   public class pojo {
   @MongoObjectId 
   String _id;        
   String Data;


  public pojo() {

  }

   public pojo(String Data) {
    this.Data = Data;
   }


  public String getData() {
    return Data;
   }


   public String getId() {
    return _id;
   }

}

我正在使用jframe Button Action GUi

      private void findActionPerformed(java.awt.event.ActionEvent evt) {                                           

    try{

     DefaultListModel listModel = new DefaultListModel();
     DataSource ds=new DataSource(); 
     ds.DataSource();

        Iterable pojo=ds.consol();
        pojo p;
        int i=0;
        for (Iterator it=pojo.iterator();it.hasNext();){  
        p = (pojo)it.next();
         listModel.addElement(p.getData());

        }
      mdata.setModel(listModel); //jlist

    }catch(Exception e){
        System.out.println(e);

    }
} 

结果

Result

0 个答案:

没有答案