Getting started using MongoDB

A: MonoDB brief introduction

MongoDB is an intermediate relational database and non-relational databases between database and is written in C ++, his advantage is supported query format is particularly strong, can store more complex data types, support for indexing

 

Two: Download

Official address: https: //www.mongodb.com/
This tutorial download 3.4 version: http: //downloads.mongodb.org/win32/mongodb-win32-x86_64-2008plus-ssl-v3.4-latest-signed.msi
 
Three: Installation and Startup Service
1, MongoDB After installation, create a database path (data directory) at the same level in the bin directory, log path (logs directory) and log files (mongo.log file)
2. Create and edit the configuration file: mongo.conf
# Database path 
dbpath = d: \ MongoDB \ Server \ 3.4 \ the Data 
# log output file path 
logpath = d: \ MongoDB \ Server \ 3.4 \ logs \ mongo.log 
# error log is in append mode 
logappend = to true 
# Enable log file, enabled by default / dʒɜːnl / log, diary 
. TECHNOLOGY iNFORMATION = to true 
# this option can filter out some useless log information, if necessary debug set to false 
quiet to true = 
# the default port number is 27017 
port = 27017

3, go to the bin directory, enter the command line window with administrator, execute the following command to install

mongod.exe --config " xxxx/mongo.conf(路径) " --install

4, executes a corresponding command

net start MongoDB # start MongoDB 
 NET STOP # MongoDB close MongoDB 
 "..... / mongod.exe" --remove # remove MongoDB

5. Verify successful start

Visit: http: // localhost: 27017 MongoDB query page

 

Four: For client installation:

There are many clients for MongoDB can download their own, I recommend that nosqlbooster, download address: https: //nosqlbooster.com/downloads

 

Five: java simple use of MongoDB

1, the service connection formats

mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

Corresponding Glossary: 

  mongodb: // prefix fixed
  username: account number, do not fill
  password: password, do not fill
  host: the host name or ip address, only the host host name is incorrect.
  port: Port, time to fill, the default 27017
  / Database: a database connected to a
  ? Options: the connection parameters, key / value pair
 
Example:
1, mongodb: // localhost port connected to the local database 27017 
2, mongodb: // root: itcast @ localhost user name root password for the database 27017 itcast connect local port 
3, mongodb: // localhost, localhost : 27018, localhost: 27019 , three connections from the master server, port 27017,27018,27019
 
2, add dependencies
 <dependency>
     <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.4.3</version>
</dependency>

3, the code test connection

   // test connection 
    @Test 
    public void testConnect () { 
        // create a client 
        MongoClient mongoClient new new MongoClient = ( "localhost", 27017); 
        // resume connection using the connection string 
        // MongoClientURI connecting = new MongoClientURI ( " mongodb: / / localhost: 27017 / test ") ; // remember the name of the database 
        // MongoClient Client = new new MongoClient (Connecting); 
        MongoDatabase database = mongoClient.getDatabase (" the Test "); // get the database 
        MongoCollection <Document> collection = database. getCollection ( "student"); // table corresponding to the acquired 
        Document document = collection.find () first ( );. // obtain a corresponding row 
        String = document.toJson JSON (); 
        System.out.println (JSON); 
    }

  Note: When using a URI connection, plus the name of the database, if the following error occurs, [MongoCommandException: Command failed with error 18:. 'Authentication failed], then please check the address and the user's connection is whether the database user belongs, in MongoDB, a user corresponds to a library, write the corresponding connection should

 

 

Six: MongoDB involve command

As well as a collection of related databases # 
1, show dbs query all database 
2, use dbbase_name handover or create a database 
3, db.dropDatabase () delete the database, first switch to the corresponding database 
4, db.createCollection (name, Options), 
5 , db.collectionName.drop () delete set 
6, db.collectionName.insert ({ "key" : "value"}) adding the collection of data 
, db.collectionName.update ({condition} update, updated content {7} , options) 
8, db.collectionName.remove ({condition} delete) to delete a document according to conditions 
9, db.collectionName.remove () to delete all the documents 
10, db.collectionName.find ({query}), no conditions All documents is to query 
11, db.collectionName.find ({query}, {field to be displayed, to be displayed is one, do not show a 0, no double quotes}), the query projection 

# relevant user 
1 , switch to the corresponding database: use the databaseName 
2, sb.createUser ({ 
        User: "username", 
        pwd: "password", 
        Roles: [
        {Role: "Role", db: "Database name"}Database name "} 
    ]
}) 
Show the Users: Query User 
db.dropUser ( "username") delete the user 
db.updateUser ( "User Name", {roles: [{role : " Role", db: "database role"}]}) modify the user information 
db.changeUserPassword ( "user name", "new password")

  

 

 

Guess you like

Origin www.cnblogs.com/gdhzdbh/p/11531017.html