java操作mongodb

來源:互聯網
上載者:User

標籤:dao層   i++   mongo   pat   條件   from   collect   模板   欄位   

上文把mongodb整合到spring,並將mongodb模板注入到dao層。

那麼下面我們進行一些操作。

optionmongo.java 。

DB  db=null;DBCollection coll=null; db=mongoTemplate.getDb(); coll= db.getCollection("test");//全量尋找,顯示name,age,隱藏id屬性。類似於select name,age from stu. DBCursor cur = coll.find(new BasicDBObject(),new BasicDBObject("name",1).append("id", 0).append("age", 1)).limit(500);       while (cur.hasNext()) {                        DBObject object = cur.next();                          System.out.println(object.get("name"));                    }//指定條件尋找。 DBObject dbo = new BasicDBObject();   dbo.put("id", 110);DBCursor cur = coll.find(dbo,new BasicDBObject("name",1).append("id", 0).append("age", 1)).limit(500);       while (cur.hasNext()) {                        DBObject object = cur.next();                        System.out.println(object.get("name"));                    }//對查詢結果進列欄位顯示更改。        while (cur.hasNext()) {                        DBObject object = cur.next();              DBObject newdbo=new BasicDBObject();              newdbo.put("姓名",object.get("name"))                        System.out.println(newdbo.get("姓名"));                    }//去重查詢BasicDBList ages = new BasicDBList(); //欄位為集合,要求條件在集合裡//將條件加入集合ages.add(age);//設定條件類 age欄位集合裡 有對應的條件。tjdbo.put("age", new BasicDBObject("$in",ages));//name去重顯示List bws=coll.distinct("name",tjdbo);              for (int i = 0; i < bws.size(); i++) {                  DBObject dbo=new BasicDBObject();                   dbo.put("name",bws.get(i)) ;                    System.out.println(dbo.get("name"));            } //模糊尋找//定義一個匹配規則Pattern pattern = Pattern.compile("^.*" + key +".*$", Pattern.CASE_INSENSITIVE);//定義條件類 DBObject dbo = new BasicDBObject();dbo.put("name", pattern);cur = coll.find(dbo,new BasicDBObject("name",1).append("id", 0).append("age", 1));

 


 

java操作mongodb

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.