MongoDB Helloworld For Window

  1. 下载MongoDB

Download MongoDB Community Server | MongoDB

  1. 安装MongoDB
  1. 创建DB.
  1. 用java code 连接mongo. 做增删改查操作。

pom.xml

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上增加记录

java 复制代码
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);
        }
    }
}

修改

java 复制代码
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);
        }
    }
}

删除

java 复制代码
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);
        }
    }
}

查询

java 复制代码
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 上可以查看数据

相关推荐
7***68431 小时前
Spring Boot 从 2.7.x 升级到 3.3注意事项
数据库·hive·spring boot
L***d6701 小时前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
java_logo1 小时前
MySQL Server Docker 容器化部署指南
linux·运维·数据库·docker·容器
likuolei1 小时前
XSL-FO 软件
java·开发语言·前端·数据库
p***95001 小时前
Springboot3 Mybatis-plus 3.5.9
数据库·oracle·mybatis
CS_浮鱼1 小时前
【MySQL】InnoDB存储引擎
数据库·mysql
合作小小程序员小小店1 小时前
桌面开发,在线%信息管理%系统,基于vs2022,c#,winform,sql server数据。
开发语言·数据库·sql·microsoft·c#
q***18841 小时前
解决phpstudy无法启动MySQL服务
数据库·mysql·adb
e***95641 小时前
【HTML+CSS】使用HTML与后端技术连接数据库
css·数据库·html
卡提西亚1 小时前
数据库笔记-4-SQL语言之DCL
数据库·笔记·sql