Java Operation MongoDB

Source: Internet
Author: User
Tags mongodb

The above integrates MongoDB into spring and injects the MongoDB template into the DAO layer.

So let's do some of the following.

Optionmongo.java.

DB db=null;dbcollection Coll=null; Db=mongotemplate.getdb (); Coll= db.getcollection ("test");//full-volume lookup, display name,age, hide id attribute. Similar to 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")); }//specifies a condition lookup.   DBObject dbo = new Basicdbobject (); Dbo.put ("id", "a");D bcursor 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"));        }//The field display changes to the query results.              while (Cur.hasnext ()) {DBObject object = Cur.next ();              DBObject newdbo=new basicdbobject (); Newdbo.put ("Name", Object.get ("name")) System.Out.println (Newdbo.get ("name")); }//to re-query basicdblist ages = new Basicdblist (); The fields are set, requiring conditions to be added to the set//To add conditions to the set Ages.add (age);//Set conditional class The age field set has the corresponding conditions.              Tjdbo.put ("Age", New Basicdbobject ("$in", Ages))//name go back to the 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")); }//Fuzzy Lookup//define a matching rule pattern pattern = Pattern.compile ("^.*" + key + ". *$", pattern.case_insensitive);//define conditional class DBObject dbo = n EW Basicdbobject ();d bo.put ("name", pattern), cur = coll.find (dbo,new basicdbobject ("name", 1). Append ("id", 0). Append (" Age ", 1));


Java Operation MongoDB

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.