1. 下载MongoDB
Download MongoDB Community Server | MongoDB
2. 安装MongoDB
3. 创建DB.
4. 用java code 连接mongo. 做增删改查操作。
pom.xml
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>4.3.3</version> <!-- Make sure to use the latest version -->
</dependency>
在User collection上增加记录
package org.example;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
public class MongoDBSaveDataExample {
public static void main(String[] args) {
try (MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017")) {
MongoDatabase database = mongoClient.getDatabase("test");
System.out.println("Connected to the database successfully.");
// Access the collection (if it doesn't exist, MongoDB will create it)
database.getCollection("users").insertOne(new Document("name", "John Doe")
.append("age", 30)
.append("email", "john.doe@example.com"));
System.out.println("Data saved successfully.");
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
}
修改
package org.example;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.result.UpdateResult;
import org.bson.Document;
public class MongoDBUpdateDataExample {
public static void main(String[] args) {
try (MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017")) {
MongoDatabase database = mongoClient.getDatabase("test");
System.out.println("Connected to the database successfully.");
// Update data
UpdateResult updateResult = database.getCollection("users")
.updateOne(Filters.eq("name", "John Doe"),
new Document("$set", new Document("age", 31)));
System.out.println("Matched count: " + updateResult.getMatchedCount());
System.out.println("Modified count: " + updateResult.getModifiedCount());
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
}
删除
package org.example;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.result.DeleteResult;
import org.bson.Document;
public class MongoDBDeleteDataExample {
public static void main(String[] args) {
try (MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017")) {
MongoDatabase database = mongoClient.getDatabase("test");
System.out.println("Connected to the database successfully.");
// Delete data
DeleteResult deleteResult = database.getCollection("users")
.deleteOne(new Document("name", "John Doe"));
System.out.println("Deleted count: " + deleteResult.getDeletedCount());
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
}
查询
package org.example;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.model.Filters;
import com.mongodb.client.FindIterable;
import org.bson.Document;
public class MongoDBQueryDataExample {
public static void main(String[] args) {
try (MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017")) {
MongoDatabase database = mongoClient.getDatabase("test");
System.out.println("Connected to the database successfully.");
// Query data
FindIterable<Document> iterable = database.getCollection("users")
.find(Filters.eq("name", "John Doe"));
// Iterate through the result set
for (Document document : iterable) {
System.out.println("Name: " + document.getString("name"));
System.out.println("Age: " + document.getInteger("age"));
System.out.println("Email: " + document.getString("email"));
}
} catch (Exception e) {
System.err.println("Error: " + e);
}
}
}
在MongoDB ui 上可以查看数据