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

相关推荐
ROCKY_8171 小时前
Mysql复习(二)
数据库·mysql·oracle
问道飞鱼3 小时前
【知识科普】认识正则表达式
数据库·mysql·正则表达式
HaiFan.3 小时前
SpringBoot 事务
java·数据库·spring boot·sql·mysql
水根LP493 小时前
linux系统上SQLPLUS的重“大”发现
数据库·oracle
途途途途4 小时前
精选9个自动化任务的Python脚本精选
数据库·python·自动化
04Koi.4 小时前
Redis--常用数据结构和编码方式
数据库·redis·缓存
silver98865 小时前
mongodb和Cassandra
数据库
PersistJiao5 小时前
3.基于 Temporal 的 Couchbase 动态 SQL 执行场景
数据库·sql
上山的月5 小时前
MySQL -函数和约束
数据库·mysql
zhcf5 小时前
【MySQL】十三,关于MySQL的全文索引
数据库·mysql