Mango Db:mongodb

Source: Internet
Author: User

Mango Db:mongodb
First, Introduction:
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, it is a high-performance, open-source, modeless, document-based database that is a popular one in today's NoSQL databases. It can be used in many scenarios to replace the traditional relational database or key/value storage methods.

NOSQL, the full name is not-only Sql, refers to the non-relational database. The next generation of databases mainly solves several key points: non-relational, distributed, open-source, horizontally extensible. The original goal was for large-scale Web applications, which began in the early 2009 with features such as freedom of mode, support for simple replication, simple APIs, eventual consistency (non-acid), high-capacity data, and so on. NoSQL is the most used when the number of key-value storage, of course, there are other document-type, column storage, graph database, XML database and so on.

Second, download the installation

1. Follow the operating system download http://www.mongodb.org/downloads

2. Create a new MongoDB folder on the D drive (this folder is the custom database installation directory D:\MongoDB) unzip the compressed package you just downloaded and copy the Bin folder to the MongoDB folder.

3. Create a new logs folder within the MongoDB folder (for storing log file D:\MongoDB\logs) and create a new empty file Mongodb.log within this folder.

4. Create a new DB folder within the MongoDB folder (to hold the database file D:\MongoDB\db).

5. Start MongoDB.
Run cmd as system Administrator and switch to D:\MongoDB\bin directory input Mongod.exe--dbpath=d:\mongodb\db. If you see the last line of the console similar to Tue Oct 11:50:55 [websvr] Admin Web console watiing for connections on port 28017 instructions to start successfully (MongoDB occupies system 27017 port) 。

6. Register MongoDB as a system service (this step must run CMD as system administrator, otherwise an error will be made)
Run the cmd input as a system administrator and switch to the bin directory of MongoDB to run the following statement

Mongod.exe--dbpath=d:\mongodb\db--logpath=d:\mongodb\logs\mongodb.log--install--servicename "MongoDB"  

Enter
If the console appears similar to the Tue Oct 12:05:15 Service can be started in the command line with ' net start MongoDB ' statement, the service has been registered successfully.
cmd--net start MongoDB (Mongo db started)
Enter http://localhost: port number in the browser-you can monitor the running state of MongoDB.

7. Test MongoDB
Create a new CMD window as an administrator, enter the bin directory of MongoDB into MONGO, as shown in the connecting to:test instructions to pass the test.
Note: You can also configure environment variables in MONGODB environment variable here: Mongodb_home=d:\mongodb
Configuring MONGODB Path, for%mongodb_home%\bin
This allows the Cmd--mongo to run
to continue testing:
(1). Enter use Test return
(2). Input Db.foo.save ({hello:1, baie:2}) Enter
(3). Enter Db.foo.find () carriage return
if a similar {"_id" appears: ObjectId ("5073a0a090f93be1455461d2"), "Hello": 1, "Baie": 2}, the test success data has been inserted into the database, and then enter exit.

8. Enter the net start MongoDB in cmd to start the MongoDB database service, when the console output MONGO DB service has started successfully, indicating that the system started successfully.

9. If a system error occurs 1067 Please remove the Mongod.lock file from the DB directory and re-enter the net start MongoDB service.

Third, download driver (for JAVA)
Https://github.com/mongodb/mongo-java-driver/downloads

Iv. examples

DEMO01:

1  Public classDemo01 {2      Public Static voidMain (string[] args)throwsException {3Mongo conn =NewMongo ("localhost", 27017); 4DB db = Conn.getdb ("Test"); 5   6         //1. Query all the database7          for(String name:conn.getDatabaseNames ()) {8System.out.println ("DbName:" +name); 9         }  Ten    One         //2. Get all collection A          for(String s:db.getcollectionnames ()) { - System.out.println (s);  -         }   the    -         //3. Querying all user Information -Dbcollection users = db.getcollection ("Users");  -Dbcursor cur =Users.find ();  +          while(Cur.hasnext ()) { - System.out.println (Cur.next ());  + System.out.println (Cur.count ());  A System.out.println (Cur.getcursorid ());  at System.out.println (json.serialize (cur));  -         }   -      }   -  }   -  

DEMO02:

1  Public classDemo01 {2      Public Static voidMain (string[] args)throwsException {3Mongo conn =NewMongo ("localhost", 27017); 4DB db = Conn.getdb ("Test"); 5Dbcollection coll = db.getcollection ("Testcollection"); 6         //Get a collection7Basicdbobject doc =NewBasicdbobject (); 8Doc.put ("name", "MongoDB"); 9Doc.put ("type", "Database"); TenDoc.put ("Count", 1);  OneBasicdbobject info =NewBasicdbobject ();  AInfo.put ("FileName", "Momo.doc");  -Info.put ("type", ". Doc");  -Info.put ("Size", "2500KB");  theDoc.put ("Info", info);  -Coll.insert (DOC);//1. Save Document -    -         //2. Querying document +DBObject MyDoc =Coll.findone ();  - System.out.println (MYDOC);  +    A         //inserting multiple document at          for(inti = 0; I <= 2; i++) {   -Coll.insert (NewBasicdbobject (). Append ("I", i));  -         }   -    -         //calculate the number of document - System.out.println (Coll.getcount ());  in    -         //Use the Dbcursor class to get all document toDbcursor Cur01 =Coll.find ();  +          while(Cur01.hasnext ()) { - System.out.println (Cur01.next ());  the         }   *    $         //Query with conditionsPanax NotoginsengBasicdbobject query =NewBasicdbobject ();  -Query.put ("I", 2);//Check I for all objects with a value of 98 theCur =coll.find (query);  +          while(Cur.hasnext ()) { A System.out.println (Cur.next ());  the         }   +    -         //parsing strings in JSON format $Object o = json.parse ("{\" name\ ": \" mongodb\ ", \" type\ ": \" database\ ", \" count\ ": 1, \" info\ ": \" sdfsdf\ "}");  $Basicdbobject oo =(Basicdbobject) o;  -System.out.println (Oo.get ("name"));  -    }   the }   -   Wuyi Console output: the{"_id": {"$oid": "51e7b3c63cfdaee4a00f7aaf"}, "name": "MongoDB", "type": "Database", "Count": 1, "info": {"fi Lename ":" Momo.doc "," type ":". Doc "," Size ":" 2500KB "}}   -3 Wu{"_id": {"$oid": "51e7ad053cfdb7a5c7796fb1"}, "I": 2}   -Mongodb

Mango Db:mongodb

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.