MongoDB for Java and CRUD operations

Source: Internet
Author: User
Tags bulk insert mongodb

MongoDB: Database, collection, document, where "set" is the corresponding relational database "table", "document" corresponding to the "line".
1, first of all, download the MongoDB Java-supported driver package

<dependency>
                      <groupId> org.mongodb</groupid >
                      <artifactId> mongo-java-driver </ artifactid>
                      <version> 2.9.3</version>
               </dependency>

2. Java Connection MongoDB Database
/**
URL Database IP Address
Port ports
Dbnname database name
Tmcinfo Collection Name
**/
Mongo Mongo = new Mongo (Url,integer.parseint (port));
                 	 db = Mongo.getdb (dbname);
                                 if (db.authenticate (user, Pwd.tochararray ()) {return
                                      true;
                         	 }
                 	  Dbcollection users = db.getcollection ("Tmcinfo");
3. CRUD operation
Query
Dbcursor cur = users.find ();
            while (Cur.hasnext ()) {
                  System.out.println (Cur.next ());
            }
Query deletion: Query for age=25 data and delete
Dbcollection.findandremove (New Basicdbobject ("Age", 25))

Insert
  Basicdbobject document = new Basicdbobject ();
  Document.put ("id", 1001);
  Document.put ("msg", "Hello World MongoDB in Java");
  Dbcollection dbcollection = db.getcollection (table_name);
  Dbcollection. Insert (document);

BULK INSERT: A BULK insert of a single TCP request, which avoids the overhead of a fragmented request.
list<dbobject> list = new arraylist<dbobject> ();
Dbcollection. Insert (list). GETN ());//getn () Affect number of rows

Delete:
Dbcollection. Remove (New Basicdbobject ("Age", New Basicdbobject ("$gte")). GETN ())
Deletes the entire collection dbcollection.remove (); Slower than db.drop_collection (table_name), which deletes the collection directly, but all indexes are also deleted
Modify:
Dbcollection. Update (New Basicdbobject ("_id", New ObjectId ("4dde2b06feb038463ff09042")), New Basicdbobject ("Age", 121 )

Returns the updated document query age=26 data, and modifies the value of name to ABC
Dbcollection.findandmodify (New Basicdbobject ("Age,"), New Basicdbobject ("name", "ABC"));

Java-mongodb

Address of API documentation

http://api.mongodb.org/java/

Official Entry Address Http://www.mongodb.org/display/DOCS/Java+Tutorial

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.