MongoDB installation on Windows and additions and deletions in Java

Source: Internet
Author: User
Tags findone mongoclient mongodb server

installation creation under 1.windows

Http://tech.it168.com/a2011/0603/1200/000001200064_2.shtml

http://blog.csdn.net/xiaoxing598/article/details/54233044

2.Java Operation MongoDB

Http://www.cnblogs.com/zhwl/p/3491213.html

http://blog.csdn.net/xiaoyw71/article/details/49633831

http://blog.csdn.net/mcpang/article/details/8730849

A To define, use:

MongoDB is a database based on distributed file storage. Written by the C + + language. Designed to provide scalable, high-performance data storage solutions for WEB applications.

MongoDB stores data as a document and data structures consist of key-value (key=>value) pairs. a MongoDB document is similar to a JSON object. Field values can contain other documents, arrays, and array of documents.

MongoDB provides a document-oriented storage that is simple and easy to operate.

you can be in index of any attribute set in the MongoDB record ( e.g.firstname= "Sameer", address= "8 Gandhi Road") To achieve a faster sort.

You can create data mirroring either locally or through the network, which makes MongoDB has a stronger extensibility.

If the load increases (requires more storage space and stronger processing power) , it can be distributed on other nodes in the computer network. This is called sharding.

the Mongo supports rich query expressions. Query directives use a JSON -style tag to easily query objects and arrays embedded in the document.

MongoDb uses the update () command to implement a replacement of the completed document (data) or some specified data fields.

the map/reduce in Mongodb is primarily used for batch processing and aggregation of data.

Map and Reduce. the Map function calls emit (Key,value) to iterate through all the records in the collection, adding key to the Value is passed to the Reduce function for processing.

The Map function and the Reduce function are written using Javascript and can be db.runcommand or mapreduce command to perform a mapreduce operation.

Gridfs is A built-in feature in MongoDB that can be used to store a large number of small files.

MongoDB allows you to execute scripts on the server , write a function in Javascript, execute directly on the server, or store the definition of the function on the server, next time you call it directly.

MongoDB supports a variety of programming languages : RUBY,PYTHON,JAVA,C + + , PHP , C # and many other languages.

MongoDB installation is simple.

Two Installation creation:

Download First:http://www.mongodb.org/downloads

install after download, here my installation path is the default road strength: C:\Program Files\mongodb

Open Bin directory:C:\Program files\mongodb\server\3.4\bin

Next we create a location where the data is stored: Custom, Mine is D:\mymongodb\data

Then run the command line as an administrator:

① Run command:mongod--dbpath "D:\mymongodb\data"

② Run command:mongod--dbpath "D:\mymongodb\data"--logpath "D:\mymongodb\logs.txt"--install--servicename "MongoDB ”

Next:cmd open the services.msc command to view the service

Mongod--remove--servicename "MongoDB"

Note that the installation was successful, right-click to start the service.

Three Manipulating MongoDB in Java

A) first download the Java driver for Jmongodb

    1. http://central.maven.org/maven2/org/mongodb/
    2. Select the same version
    3. Click http://api.mongodb.org/java/current/index.html to learn more about Java-driven API information
    4. Download a few three files

mongo-Java-driver-2.11.4.jar
Mongo-java-driver-2.11.4-javadoc.jar
Mongo-java-driver-2.11.4-sources.jar

through maven dependency

<dependency>

<groupId>org.mongodb</groupId>

<artifactId>mongo-java-driver</artifactId>

<version>2.11.4</version>

</dependency>

b) use: Refer to the Testclass.java class in the Mongodbtest project and the Zsgc.java class.

    1. http://blog.csdn.net/mcpang/article/details/8730849

Package com;

Import java.net.UnknownHostException;
Import Java.util.Arrays;
Import Java.util.Set;

Import Org.junit.Test;

Import Com.mongodb.BasicDBObject;
Import Com.mongodb.DB;
Import com.mongodb.DBCollection;
Import Com.mongodb.DBObject;
Import Com.mongodb.Mongo;
Import com.mongodb.MongoClient;
Import com.mongodb.ServerAddress;

public class testclass{

public static void Main (string[] args) throws Unknownhostexception {
Mode one: direct connection to a single MongoDB server (note that this way does not automatically discover the primary server in the MongoDB cluster)
Mongoclient mongoClient1 = new Mongoclient ();
Mongo mongoClient1 = new Mongo ();
Mode two (specify IP):
Mongoclient MongoClient2 = new Mongoclient ("localhost");
Mode two (specify IP, port):
Mongoclient MongoClient3 = new Mongoclient ("localhost", 27017);
Mode three: Connect to a MONGODB server cluster (will automatically discover the primary server)
Mongoclient mongoClient4 = new Mongoclient (arrays.aslist (New serveraddress ("localhost", 27017),
New ServerAddress ("localhost", 27018),
New ServerAddress ("localhost", 27019));

DB db = Mongoclient1.getdb ("local");
Dbcollection users = db.getcollection ("users");
/**
* DBObject represents the document, this is an interface, Java provides a variety of implementations, the simplest is basicdbobject
*/
DBObject user = new Basicdbobject ();
User.put ("Name", "Jimmy");
User.put ("Age", "34");
DBObject address = new Basicdbobject ();
Address.put ("City", "BJ");
Address.put ("Street", "Bq Road");
Address.put ("Mail", "Ufpark 68#");
Address.put ("Test", "test result");
/**
* For inline documents, we need to populate the embedded documents before populating the outer documents!
*/
User.put ("Address", address);
Insert the document into the collection
Users.insert (user);
Query the data from the collection, we query one, call FindOne can
DBObject DbUser = Users.findone ();
System.out.println ("name" + ":" + dbuser.get ("name"));
System.out.println ("Age" + ":" + dbuser.get ("age"));
DBObject dbaddress = (dbobject) user.get ("Address");
System.out.println ("City" + ":" + dbaddress.get ("city"));
SYSTEM.OUT.PRINTLN ("Street" + ":" + dbaddress.get ("street"));
System.out.println ("Mail" + ":" + Dbaddress.get ("Mail"));
Gets all the collections on the MongoDB that cannot be implemented.
Set<string> colls = Db.getcollectionnames ();
for (String s:colls) {
System.out.println (s+ "dsdsfdf");
// }

Get a single collection
Call the GetCollection (String CollectionName) method of db to get a single collection
Dbcollection coll = db.getcollection ("Collname");
System.out.println (coll);
}

public void Creatconnect () throws exception{
Mode one: direct connection to a single MongoDB server (note that this way does not automatically discover the primary server in the MongoDB cluster)
Mongoclient mongoClient1 = new Mongoclient ();
Mode two (specify IP):
Mongoclient MongoClient2 = new Mongoclient ("localhost");
Mode two (specify IP, port):
Mongoclient MongoClient3 = new Mongoclient ("localhost", 27017);
Mode three: Connect to a MONGODB server cluster (will automatically discover the primary server)
Mongoclient mongoClient4 = new Mongoclient (arrays.aslist (New serveraddress ("localhost", 27017),
New ServerAddress ("localhost", 27018),
New ServerAddress ("localhost", 27019));

DB db = Mongoclient1.getdb ("local");

Get to all collections on MongoDB
Set<string> colls = Db.getcollectionnames ();
for (String s:colls) {
System.out.println (s+ "dsdsfdf");
}

Get a single collection
Call the GetCollection (String CollectionName) method of db to get a single collection
Dbcollection coll = db.getcollection ("testcollection");


}


}

Package com;

Import Java.util.Date;

Import Com.mongodb.BasicDBObject;
Import Com.mongodb.DB;
Import com.mongodb.DBCollection;
Import Com.mongodb.DBCursor;
Import Com.mongodb.Mongo;

public class ZSGC {

public static void Main (string[] args) {
try {


Establish connection, no parameter default is ("localhost", 27017)
Mongo Mongo = new Mongo ("127.0.0.1", 27017);


/**** Get Database db ****/
If the database does not exist, create a
DB db = Mongo.getdb ("Testmongodb");
Database name, password.
Boolean OK = db.authenticate ("root", "root". ToCharArray ());
if (OK) {
SYSTEM.OUT.PRINTLN ("DB connection success! ");
//
// }{
SYSTEM.OUT.PRINTLN ("DB connection Fail! ");
// }
/**** gets the collection of tables equivalent to MySQL, "user" for the table name ****/
If the collection does not exist, create a
Dbcollection table = db.getcollection ("user");

/**** Increase ****/
Create a text to store Key,value
Basicdbobject document = new Basicdbobject ();
Document.put ("name", "Mkyong");
Document.put ("Age", 30);
Document.put ("CreatedDate", New Date ());
Table.insert (document);

/**** Cha ****/
Basicdbobject searchQuery = new Basicdbobject ();
Searchquery.put ("name", "Mkyong");

dbcursor cursor = Table.find (searchQuery);

while (Cursor.hasnext ()) {
System.out.println (Cursor.next ());
}

/**** Change ****/
Query to the text according to queries, and then modify the text.
Basicdbobject query = new Basicdbobject ();
Query.put ("name", "Mkyong");

Basicdbobject newdocument = new Basicdbobject ();
Newdocument.put ("name", "mkyong-updated");

Basicdbobject updateobj = new Basicdbobject ();
Updateobj.put ("$set", newdocument);

Table.update (query, updateobj);

/**** Delete ****/
Query to text and then delete. Dbcursor text
Basicdbobject SearchQuery2
= new Basicdbobject (). Append ("name", "mkyong-updated");

Dbcursor Cursor2 = Table.find (SearchQuery2);

while (Cursor2.hasnext ()) {
System.out.println (Cursor2.next ());
Table.remove (SearchQuery2);
}
/**** Delete Data Set ****/
Db.getcollection ("User"). Drop ();

/**** done ****/
System.out.println ("Done");

} catch (Exception e) {
E.printstacktrace ();
}
}

}

MongoDB installation on Windows and additions and deletions in Java

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.