Some concepts of MongoDB
The following is a comparison between MongoDB and MySql
MySQLMongoDB
database (database) db (database)
table(table) collection(collection)
row document
column(column)field(field)
primary key (specify primary key) "_id" : ObjectId("******") Automatically generates built-in primary key
The Mysql data form is as follows
The same data is similar to JSON key-value pairs in MongoDB, called BSON, as follows
CRUD of MongoDB
After the installation in Windows is successful, use the mongod command to start the service and use the mongo command to connect to the test library by default.
1. Create a collection
Create user collection: db.createCollection("user") //No need to define fields
2. Insert the record with name=wangxu,age=27: db.user.insert({"name":"wangxu","age":27})
3. Query all queries: db.user.find()
Query the record of name=wangxu: db.user.find({"name":"wangxu"})
4. Update the record age of name=wangxu to 30: db.user.update({name:"wangxu"},{"$set":{"age":30}})
5. Delete the record with name=wangxu: db.user.remove({"name":"wangxu"})
Delete all: db.user.remove()
6. Delete the collection and delete the user collection: db.user.drop()
Java calls MongoDB
After introducing the Java driver package of MongoDB, the test code is as follows:
package com.wx.test;import java.util.ArrayList;import java.util.List;import org.bson.Document;import com.mongodb.MongoClient;import com.mongodb.client.FindIterable;import com.mongodb.client.MongoCollection;import com.mongodb.client.MongoCursor;import com.mongodb.client.MongoDatabase;import com.mongodb.client.model.Filters;/** * Test mongodb Test points: add, query, update, delete * * @author wangxu * @date 2016-3-27 */public class TestMongoDB { public static void main(String[] args) { // Establish a connection, MongoDB does not have a username and password by default MongoClient mongoClient = new MongoClient("localhost", 27017); // Get the test library MongoDatabase db = mongoClient.getDatabase("test"); // Create collection db.createCollection("user"); // Get collection MongoCollection collection = db.getCollection("user"); // Insert document (support multiple lines) Document document = new Document("name", "wangxu").append("age", "27"); Document document2 = new Document("name", "wangxu_java").append("age", "27"); List<Document> documents = new ArrayList<>(); documents.add(document); documents.add(document2); collection.insertMany(documents); // Update wangxu's age to 30 collection.updateMany(Filters.eq("name", "wangxu"), new Document( "$set", new Document("age", 30)); // Query the document FindIterable<Document> findIterable = collection.find(); MongoCursor<Document> mongoCursor = findIterable.iterator(); // traverse the cursor while (mongoCursor.hasNext()) { Document doc = mongoCursor.next(); System.out.println("name=" + doc.get("name")); } // Delete wangxu_java collection.deleteMany(Filters.eq("name", "wangxu_java")); }}A question about Java driver package
MongoClient mongoClient = new MongoClient("Domain does not exist", 27017); //Create a connectionWhen I saved the above code, I found that there was no need to handle exceptions. At first, I thought the exception was designed as "non-checked exceptions" and would be thrown when running. Finally, the test found that no exceptions would be thrown at all. The exceptions would be thrown during subsequent CRUD operations, but they were almost all non-checked exceptions. I personally feel that this design is a bit incompatible with the exception classification mechanism of Java?