Java Operation MongoDB Implement curd function Instance _java

Source: Internet
Author: User
Tags mongodb mongodb server

First download the corresponding driver: official online Download: http://central.maven.org/maven2/org/mongodb/mongo-java-driver/
This article uses the Mongo-2.10.1.jar version:

There are detailed code below, and each method can be executed separately. In order to make it easy to run, the database is relink in each method, but not again, because the purpose of this is to know MongoDB.

Copy Code code as follows:

Package Com.mongo.dao;
Import Com.mongodb.BasicDBObject;
Import Com.mongodb.DB;
Import com.mongodb.DBCollection;
Import Com.mongodb.DBCursor;
Import Com.mongodb.Mongo;

/**
* Storage testing of basic entities
* @author Lhy
*
*/
public class Entitytest {

public static void Main (string[] args) throws exception{
Delete ();
}

/**
* Save entity objects
* @throws Exception
*/
public static void Saveentity () throws exception{
First: Instantiate the MONGO object, and connect the MongoDB server containing all the databases

Default construction method, default is to connect native, port number, default is 27017
Equivalent to MONGO MONGO =new MONGO ("localhost", 27017)
Mongo Mongo =new Mongo ();

Second: Connecting to specific databases
Where the parameter is the name of the specific database, if the server does not exist, will automatically create
DB db=mongo.getdb ("Mymongo");

Third: operation of specific tables
There is no concept of a table in the MongoDB, but rather a collection
Where the parameter is a table in the database, if it does not exist, it is automatically created
Dbcollection collection=db.getcollection ("user");

Add action
There is no concept of line in MongoDB, but it refers to the document
Basicdbobject document=new basicdbobject ();

Document.put ("id", 1);
Document.put ("name", "Xiaoming");
And then save it to the collection.
Collection.insert (document);


Of course, I can also save such a JSON string
/*          {
"id": 1,
"Name", "Xiaoming",
"Address":
{
"City": "Beijing",
"Code": "065000"
}
}*/
Realize the above JSON string thinking as follows:
The first: When you are like XML, you keep adding
Basicdbobject addressdocument=new basicdbobject ();
Addressdocument.put ("City", "Beijing");
Addressdocument.put ("Code", "065000");
Document.put ("Address", addressdocument);
And then save the database
Collection.insert (document);

Second: Save JSON directly to the database
/* String jsontest= "{' id ': 1, ' name ': ' Xiaoming '," +
"' Address ': {' city ': ' Beijing ', ' Code ': ' 065000 '}" +
"}";
DBObject dbobjct= (dbobject) json.parse (jsontest);
Collection.insert (DBOBJCT);
}

/**
* Iterate through all the
* @throws Exception
*/
public static void SelectAll () throws exception{
First: Instantiate the MONGO object, and connect the MongoDB server containing all the databases

Default construction method, default is to connect native, port number, default is 27017
Equivalent to MONGO MONGO =new MONGO ("localhost", 27017)
Mongo Mongo =new Mongo ();

Second: Connecting to specific databases
Where the parameter is the name of the specific database, if the server does not exist, will automatically create
DB db=mongo.getdb ("Mymongo");

Third: operation of specific tables
There is no concept of a table in the MongoDB, but rather a collection
Where the parameter is a table in the database, if it does not exist, it is automatically created
Dbcollection collection=db.getcollection ("user");

Query operations
Query all
Which is similar to the concept of Access database midstream
Dbcursor Cursor=collection.find ();
System.out.println ("The user table in MongoDB is as follows:");
while (Cursor.hasnext ()) {
System.out.println (Cursor.next ());
}
}

/**
* Query according to the conditions
* @throws Exception
*/
public static void Selectpart () throws exception{
First: Instantiate the MONGO object, and connect the MongoDB server containing all the databases

Default construction method, default is to connect native, port number, default is 27017
Equivalent to MONGO MONGO =new MONGO ("localhost", 27017)
Mongo Mongo =new Mongo ();

Second: Connecting to specific databases
Where the parameter is the name of the specific database, if the server does not exist, will automatically create
DB db=mongo.getdb ("Mymongo");

Third: operation of specific tables
There is no concept of a table in the MongoDB, but rather a collection
Where the parameter is a table in the database, if it does not exist, it is automatically created
Dbcollection collection=db.getcollection ("user");


You can put it directly.
Basicdbobject queryobject=new basicdbobject ();
Queryobject.put ("id", 1);
Dbcursor Querycursor=collection.find (Queryobject);
SYSTEM.OUT.PRINTLN ("Condition inquiry is as follows:");
while (Querycursor.hasnext ()) {
System.out.println (Querycursor.next ());
}
}

/**
* Update operation
* Update a record
* @throws Exception
*/
public static void Update () throws exception{
First: Instantiate the MONGO object, and connect the MongoDB server containing all the databases

Default construction method, default is to connect native, port number, default is 27017
Equivalent to MONGO MONGO =new MONGO ("localhost", 27017)
Mongo Mongo =new Mongo ();

Second: Connecting to specific databases
Where the parameter is the name of the specific database, if the server does not exist, will automatically create
DB db=mongo.getdb ("Mymongo");

Third: operation of specific tables
There is no concept of a table in the MongoDB, but rather a collection
Where the parameter is a table in the database, if it does not exist, it is automatically created
Dbcollection collection=db.getcollection ("user");

The updated object
The first way of updating
Basicdbobject newbasicdbobject =new basicdbobject ();
Newbasicdbobject.put ("id", 2);
Newbasicdbobject.put ("name", "Little Red");
Collection.update (New Basicdbobject (). Append ("id", 1), newbasicdbobject);

The second way of updating
Update a field
Basicdbobject newbasicdbobject =new basicdbobject (). Append ("$set", New Basicdbobject (). Append ("name", "Little Red"));
Collection.update (New Basicdbobject (). Append ("id", 1). Append ("name", "Xiaoming"), Newbasicdbobject);


Dbcursor Querycursor1=collection.find ();
System.out.println ("Updated results are as follows:");
while (Querycursor1.hasnext ()) {
System.out.println (Querycursor1.next ());
}
}

/**
* Delete document, including delete all or delete part
* @throws Exception
*/
public static void Delete () throws exception{

First: Instantiate the MONGO object, and connect the MongoDB server containing all the databases

Default construction method, default is to connect native, port number, default is 27017
Equivalent to MONGO MONGO =new MONGO ("localhost", 27017)
Mongo Mongo =new Mongo ();

Second: Connecting to specific databases
Where the parameter is the name of the specific database, if the server does not exist, will automatically create
DB db=mongo.getdb ("Mymongo");

Third: operation of specific tables
There is no concept of a table in the MongoDB, but rather a collection
Where the parameter is a table in the database, if it does not exist, it is automatically created
Dbcollection collection=db.getcollection ("user");
Basicdbobject queryobject1=new basicdbobject ();
Queryobject1.put ("id", 2);
Queryobject1.put ("name", "Little Red");

Delete a record
Collection.remove (QUERYOBJECT1);
Remove all
Collection.drop ();

Dbcursor Cursor1=collection.find ();
SYSTEM.OUT.PRINTLN ("The result of the deletion is as follows:");
while (Cursor1.hasnext ()) {
System.out.println (Cursor1.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.