First: A brief introduction to MonoDB
MongoDB is a database between relational database and non-relational database. It is written in C++ language. Its advantage is that the query format supported by MongoDB is very powerful. It can store complex data types and support indexing.
Two: Download
#Database Path dbpath=d:\MongoDB\Server\3.4\data #Log Output File Path logpath=d:\MongoDB\Server\3.4\logs\mongo.log #Error logs adopt additional mode logappend=true #Enable Log Files, Default Enable / d nl / Logs, Diaries journal=true #This option can filter out some useless log information. If you need to debug it, set it to false. quiet=true #The default port number is 27017 port=27017
3. Enter the bin directory, use the administrator to enter the command line window, and execute the following commands for installation
mongod.exe --config " xxxx/mongo.conf(Path) " --install
4. Execute the corresponding command
net start MongoDB #Start MongoDB net stop MongoDB #Close MongoDB "...../mongod.exe" --remove #Remove MongoDB
5. Verify that the startup is successful
Access: http://localhost:27017 Query MongoDB Page
Fourth: For client installation:
Nowadays, there are many clients of MongoDB that can download by themselves. My recommendation is nosqlbooster. The download address is https://nosqlbooster.com/downloads.
5: Simple use of MongoDB in java
1. Connection format of services
mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]
Corresponding NOUN explanations:
1,mongodb://localhost Connects Local Database Port 27017 2,mongodb://root:itcast@localhost connects to the local database port 27017 using the user name root password for itcast 3,mongodb://localhost,localhost:27018,localhost:27019, connecting three master-slave servers, ports 27017, 27018, 27019
<dependency> <groupId>org.mongodb</groupId> <artifactId>mongo-java-driver</artifactId> <version>3.4.3</version> </dependency>
3. Code test connection
//Test connection @Test public void testConnect(){ //Create Client MongoClient mongoClient = new MongoClient("localhost", 27017); //Resume Connection Using Connection String // MongoClientURI connection = new MongoClientURI ("mongodb://localhost:27017/test"); //remember the name of the database //MongoClient client = new MongoClient(connecting); MongoDatabase database = mongoClient.getDatabase("test");//Getting the database MongoCollection<Document> collection = database.getCollection("student"); //Get the corresponding table Document document = collection.find().first(); //Get the corresponding row String json = document.toJson(); System.out.println(json); }
Note: When using URI to connect, add the name of the database, if the following error occurs, [Mongo Command Exception: Command failed with error 18:'Authentication'failed.], please check the address of the connection and whether the user belongs to the database, in Mong Command Exception. In oDB, a user corresponds to a library, and the corresponding library should be written at connection time.
Six: MongoDB involves commands
#Database and set correlation 1,show dbs Query all databases 2,use dbbase_name Switch or create a database 3,db.dropDatabase() To 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 data to collections 7,db.collectionName.update( {update criteria},{Updated Contents},option ) 8,db.collectionName.remove({Delete condition})Delete documents according to conditions 9,db.collectionName.remove() Delete all documents 10,db.collectionName.find({query criteria}),No condition is to query all documents 11,db.collectionName.find({query criteria},{The fields to be displayed are 1, 0 and double quotation marks.}),Projection query #User correlation 1,First switch to the corresponding database: use databaseName 2,sb.createUser({ user: "username", pwd:"Password", roles:[ { role:"role",db:"Database name" } ] }) show users: Query users db.dropUser(" User name") delete user db.updateUser("User name",{roles:[{role:"role",db: "Database role"}]}) Modify user information db.changeUserPassword("User name","New password")