Java Operations MONGO DB database example __ Database

Source: Internet
Author: User
Tags findone mongodb
Package COM.TOMPIG.LJC;
Import java.net.UnknownHostException;
Import java.util.ArrayList;
Import Java.util.Date;
Import java.util.List;
Import Java.util.Set;
Import Com.mongodb.BasicDBObject;
Import Com.mongodb.DB;
Import com.mongodb.DBCollection;
Import Com.mongodb.DBCursor;
Import Com.mongodb.DBObject;
Import Com.mongodb.Mongo;

Import com.mongodb.MongoException;
	public class Dbutil {String collectionsname = "User_info";
	String dbname = "LJC";
	Dbcollection coll = null;

	Mongo m = null;
		Public Dbutil () {try {coll = getdbcollection (collectionsname);
		catch (Exception e) {e.printstacktrace ();
		} public dbcollection getdbcollection (String collectionname) {dbcollection coll = null;
			try {m = new Mongo ("localhost", 27017);
DB db = M.getdb (dbname); if (Db.authenticate ("LJC", "007". ToCharArray ())) {//System.out.println ("Success");//} coll = Db.getcollec
		tion (collectionname);
		catch (Unknownhostexception e) {e.printstacktrace (); } catch (Mongoexception e) {e.printstacktrace ();
	return Coll;
		}//MONGO DB Delete database public void Dropdatabase () {try {m.dropdatabase (dbname);
		catch (Mongoexception e) {e.printstacktrace ();
		}//MONGO DB query all table names public void Getallcollections () {db db = M.getdb ("MyDB");
		Set<string> colls = Db.getcollectionnames ();
		for (String s:colls) {System.out.println (s);
		}//MONGO DB Query index public void Getallindex () {list<dbobject> List = Coll.getindexinfo ();
		for (DBObject o:list) {System.out.println (o);
		}//MONGO DB inserts insert public void Insert (int i) {Basicdbobject doc = new Basicdbobject ();
		Doc.put ("name", "Liu" + i);
		Doc.put ("age", + i);
		Doc.put ("Sex", "boy");
		Doc.put ("Time", New Date ());
	Coll.insert (DOC);
		public void Batchinsert () {List datas = new ArrayList ();
			for (int i = 0; i < i++) {basicdbobject bo = new Basicdbobject ();
			Bo.put ("name", "Liu");
	Bo.append ("Age", I);		Datas.add (BO);
	} coll.insert (Datas);
		//Query single public void FindOne () {Basicdbobject obj = (basicdbobject) coll.findone ();
	System.out.println (obj);
		}//MONGO DB modifies public void update () {basicdbobject query = new Basicdbobject ();
		Query.put ("name", "Liu");
		DBObject stufound = coll.findone (query);
		Stufound.put ("Name", Stufound.get ("name") + "update_1");
	Coll.update (query, Stufound);
		//query All public void Queryall () {Basicdbobject obj = new Basicdbobject ();
		Obj.put ("Sex", "boy");
		dbcursor cursor = coll.find (obj);
			while (Cursor.hasnext ()) {String name = (String) cursor.next (). Get ("name");
		SYSTEM.OUT.PRINTLN (name);
	} cursor.close ();
		}//Condition delete public void Delete (String name) {basicdbobject query = new Basicdbobject ();
		Query.put ("name", name);
		Locate and delete, and return the deleted object DBObject removeobj = coll.findandremove (query);
	System.out.println (Removeobj); }//Conditional query public void Findbyname (String name) {Basicdbobject obj = new BasicdbobJect ();
		Obj.put ("name", name);
		dbcursor cursor = coll.find (obj);
		while (Cursor.hasnext ()) {System.out.println (Cursor.next ());
		}//Conditional query 2 public void Argsfind () {Basicdbobject condition = new Basicdbobject (); 
		Condition.put ("Age", New Basicdbobject ("$gt"), Append ("$lte", 30));
		Condition.put ("Age", New Basicdbobject ("$gt", 50));
		Coll.find (condition);
	Comparer//"$GT": Greater Than//"$gte": Greater Than//"$LT": Less Than//"$lte": Less Than//"$in": included///below Query 20<age<=30}//Paging query
		public void Pagequery () {dbcursor cursor = Coll.find (). Skip (0). Limit (10);
		while (Cursor.hasnext ()) {System.out.println (Cursor.next ());
 }
	}
}


 

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.