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 上可以查看数据

相关推荐
wowocpp1 小时前
查看 磁盘文件系统格式 linux ubuntu blkid ext4
linux·数据库·ubuntu
Ai 编码助手6 小时前
MySQL中distinct与group by之间的性能进行比较
数据库·mysql
陈燚_重生之又为程序员7 小时前
基于梧桐数据库的实时数据分析解决方案
数据库·数据挖掘·数据分析
caridle7 小时前
教程:使用 InterBase Express 访问数据库(五):TIBTransaction
java·数据库·express
白云如幻7 小时前
MySQL排序查询
数据库·mysql
萧鼎7 小时前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步
^velpro^7 小时前
数据库连接池的创建
java·开发语言·数据库
荒川之神7 小时前
ORACLE _11G_R2_ASM 常用命令
数据库·oracle
IT培训中心-竺老师7 小时前
Oracle 23AI创建示例库
数据库·oracle
小白学大数据7 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫