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