MongoDB basic series -- Implement curd using MongoDB in Java

Source: Internet
Author: User
Tags mongodb server

MongoDB supports multiple languages and is driven by multiple languages.

Java MongoDB operations to achieve curd: premise: Download the corresponding driver: download on the official website: http://central.maven.org/maven2/org/mongodb/mongo-java-driver/ I use mongo-2.10.1.jar version: the following code in detail, each method can be executed separately. In order to facilitate the operation, each method re-links the database, but does not mention it again, because the purpose here is to understand MongoDB.
Package COM. mongo. dao; import COM. mongoDB. basicdbobject; import COM. mongoDB. DB; import COM. mongoDB. dbcollection; import COM. mongoDB. dbcursor; import COM. mongoDB. mongo;/*** test the storage of basic entities * @ author LHY **/public class entitytest {public static void main (string [] ARGs) throws exception {Delete ();}/*** save object * @ throws exception */public static void saveentity () throws exception {// first: instantiate a mongo object, the connection to the MongoDB server contains all the databases // default constructor, which is connected to the local machine by default and port number. The default value is 27017 // equivalent to Mongo = new Mongo ("localhost", 27017) mongo = new Mongo (); // Second: connect to a specific database // The parameter is the name of the specific database. If the server does not exist, the database = Mongo is automatically created. getdb ("mymongo"); // third: the specific table for the Operation // does not have the table concept in MongoDB, but indicates the set // Where the parameter is the table in the database. If it does not exist, dbcollection collection = dB is automatically created. getcollection ("user"); // Add operation // no row concept in MongoDB, but it refers to the document basicdbobject document = new basicdbobject (); document. put ("ID", 1); document. put ("name", "James"); // save it to the collection /// collection. insert (document); // Of course, I can also save this JSON string/* {"ID": 1, "name", "James", "Address ": {"city": "Beijing", "code": "065000"} * // The preceding JSON string is implemented as follows: // The first method is similar to XML, add basicdbobject addressdocument = new basicdbobject (); addressdocument. put ("city", "Beijing"); addressdocument. put ("code", "065000"); document. put ("Address", addressdocument); // Save the collection in the database. insert (document); // type 2: directly store JSON in the database/* string jsontest = "{'id': 1, 'name': 'xiaoming ', "+" 'address': {'city': 'beijing', 'code': '000000'} "+"} "; dbobject dbobjct = (dbobject) JSON. parse (jsontest); Collection. insert (dbobjct); */}/*** traverse all the * @ throws exception */public static void selectall () throws exception {// first: instantiate the Mongo object, the connection to the MongoDB server contains all the databases // default constructor, which is connected to the local machine by default and port number. The default value is 27017 // equivalent to Mongo = new Mongo ("localhost", 27017) mongo = new Mongo (); // Second: connect to a specific database // The parameter is the name of the specific database. If the server does not exist, the database = Mongo is automatically created. getdb ("mymongo"); // third: the specific table for the Operation // does not have the table concept in MongoDB, but indicates the set // Where the parameter is the table in the database. If it does not exist, dbcollection collection = dB is automatically created. getcollection ("user"); // query operation // query all // similar to the cursor concept dbcursor cursor = collection in the Access Database. find (); system. out. println ("the result of the User table in MongoDB is as follows:"); While (cursor. hasnext () {system. out. println (cursor. next () ;}}/*** query by condition * @ throws exception */public static void selectpart () throws exception {// first: instantiate the Mongo object, the connection to the MongoDB server contains all the databases // default constructor, which is connected to the local machine by default and port number. The default value is 27017 // equivalent to Mongo = new Mongo ("localhost", 27017) mongo = new Mongo (); // Second: connect to a specific database // The parameter is the name of the specific database. If the server does not exist, the database = Mongo is automatically created. getdb ("mymongo"); // third: the specific table for the Operation // does not have the table concept in MongoDB, but indicates the set // Where the parameter is the table in the database. If it does not exist, dbcollection collection = dB is automatically created. getcollection ("user"); // you can directly put basicdbobject queryobject = new basicdbobject (); queryobject. put ("ID", 1); dbcursor querycursor = collection. find (queryobject); system. out. println ("condition query:"); While (querycursor. hasnext () {system. out. println (querycursor. next () ;}/ ***** update operation * updates a record * @ throws exception */public static void Update () throws exception {// first: instantiate the Mongo object, the connection to the MongoDB server contains all the databases // default constructor, which is connected to the local machine by default and port number. The default value is 27017 // equivalent to Mongo = new Mongo ("localhost", 27017) mongo = new Mongo (); // Second: connect to a specific database // The parameter is the name of the specific database. If the server does not exist, the database = Mongo is automatically created. getdb ("mymongo"); // third: the specific table for the Operation // does not have the table concept in MongoDB, but indicates the set // Where the parameter is the table in the database. If it does not exist, dbcollection collection = dB is automatically created. getcollection ("user"); // The updated object // The first update method basicdbobject newbasicdbobject = new basicdbobject (); newbasicdbobject. put ("ID", 2); newbasicdbobject. put ("name", "Xiaohong"); Collection. update (New basicdbobject (). append ("ID", 1), newbasicdbobject); // the second update method // update a field // basicdbobject newbasicdbobject = new basicdbobject (). append ("$ set", new basicdbobject (). append ("name", "Xiaohong"); // collection. update (New basicdbobject (). append ("ID", 1 ). append ("name", "James"), newbasicdbobject); dbcursor querycursor1 = collection. find (); system. out. println ("the updated result is as follows:"); While (querycursor1.hasnext () {system. out. println (querycursor1.next () ;}/ *** delete a document, including deleting all or some of the documents * @ throws exception */public static void Delete () throws exception {// first: instantiate the Mongo object. The connection to the MongoDB server contains all databases. // The default constructor is used to connect to the local machine and port number, the default value is 27017 // equivalent to Mongo = new Mongo ("localhost", 27017) Mongo = new Mongo (); // second: connect to a specific database // The parameter is the name of the specific database. If the server does not exist, the database DB = Mongo is automatically created. getdb ("mymongo"); // third: the specific table for the Operation // does not have the table concept in MongoDB, but indicates the set // Where the parameter is the table in the database. If it does not exist, dbcollection collection = dB is automatically created. getcollection ("user"); basicdbobject queryobject1 = new basicdbobject (); queryobject1.put ("ID", 2); queryobject1.put ("name", "Xiaohong "); // delete a collection record. remove (queryobject1); // Delete All // collection. drop (); dbcursor cursor1 = collection. find (); system. out. println ("the result after deletion is as follows:"); While (cursor1.hasnext () {system. out. println (cursor1.next ());}}}

Here, mongdb is executed using a JSON string. As for how to display the JSON string interface, the next blog will provide a demo for you to practice.

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.