BasicDBObject mongodb java ví dụ

Tìm hiểu cách tìm tài liệu trong MongoDB. Hướng dẫn tìm tài liệu mongodb này bao gồm một số cách để truy vấn một tài liệu hoặc tìm nhiều tài liệu dựa trên cùng một điều kiện như chúng ta làm trong SQL bằng cách sử dụng KHOẢN WHERE

Table of Contents

1) Select all documents from a collection
2) Select first document from a collection
3) Select single document and limited field(s) from a collection
4) Select all documents with unique id from a collection
5) Document ids IN clause example
6) Document ids less than or greater than clause example
7) Document ids NOT IN clause example
8) Documents matching multiple fields example
9) Documents matching field value to some REGEX example

MongoDB tìm tài liệu – chuẩn bị dữ liệu thử nghiệm

Để xây dựng và chạy các ví dụ được sử dụng trong hướng dẫn này, tôi đã điền 20 tài liệu nhân viên vào cơ sở dữ liệu với

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
1 từ 1 đến 20 và
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
2 từ TestEmployee_1 đến TestEmployee_10

Trong bài viết này, chúng tôi trình bày cách làm việc với MongoDB trong Java. Có một hướng dẫn Java ngắn gọn trên ZetCode

MongoDB là cơ sở dữ liệu định hướng tài liệu đa nền tảng NoSQL. Đây là một trong những cơ sở dữ liệu phổ biến nhất hiện có. MongoDB được phát triển bởi MongoDB Inc. và được xuất bản dưới dạng phần mềm mã nguồn mở và miễn phí

Một bản ghi trong MongoDB là một tài liệu, là một cấu trúc dữ liệu bao gồm các cặp trường và giá trị. Các tài liệu MongoDB tương tự như các đối tượng JSON. Giá trị của các trường có thể bao gồm các tài liệu, mảng và mảng tài liệu khác. MongoDB lưu trữ tài liệu trong bộ sưu tập. Bộ sưu tập tương tự như bảng trong cơ sở dữ liệu quan hệ và tài liệu tương tự như hàng

MongoDB đại diện cho các tài liệu

    org.mongodb
    mongo-java-driver
    x.y.z

7 ở định dạng được mã hóa nhị phân có tên là
    org.mongodb
    mongo-java-driver
    x.y.z

8 đằng sau hậu trường.
    org.mongodb
    mongo-java-driver
    x.y.z

8 mở rộng mô hình
    org.mongodb
    mongo-java-driver
    x.y.z

7 để cung cấp các loại dữ liệu bổ sung, các trường được sắp xếp và hiệu quả để mã hóa và giải mã trong các ngôn ngữ khác nhau. Các. NET sử dụng

    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


1 để đại diện cho
    org.mongodb
    mongo-java-driver
    x.y.z

8

Lệnh sau có thể được sử dụng để cài đặt MongoDB trên Linux dựa trên Debian

$ sudo apt-get install mongodb

Lệnh cài đặt các gói cần thiết đi kèm với MongoDB

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
0

Với lệnh


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


3, chúng tôi kiểm tra trạng thái của máy chủ

    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


4

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
3

Máy chủ


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


4 được khởi động bằng lệnh

    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


6

Tạo cơ sở dữ liệu

Công cụ


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


7 là giao diện shell JavaScript tương tác với MongoDB, cung cấp giao diện cho quản trị viên hệ thống cũng như cách để nhà phát triển kiểm tra các truy vấn và hoạt động trực tiếp với cơ sở dữ liệu

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
7

Chúng tôi tạo cơ sở dữ liệu


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


8 và chèn tám tài liệu vào bộ sưu tập

    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


9

Chúng tôi sử dụng khai báo Maven sau để đưa trình điều khiển Java MongoDB vào dự án của chúng tôi

    org.mongodb
    mongo-java-driver
    x.y.z

Nó là một JAR tất cả trong một, nhúng trình điều khiển lõi và BSON. BSON, viết tắt của Binary JSON, là một tuần tự hóa được mã hóa nhị phân của các tài liệu giống như JSON

Java MongoDB liệt kê các bộ sưu tập cơ sở dữ liệu

Ví dụ đầu tiên kết nối với cơ sở dữ liệu


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


8 và truy xuất các bộ sưu tập của nó


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


Đây là tệp

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
31 của chúng tôi

com/zetcode/MongoListCollections. java

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
3

Ví dụ kết nối với cơ sở dữ liệu


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


8 và truy xuất tất cả các bộ sưu tập của nó

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
6

Chúng tôi đặt mức ghi nhật ký cho MongoDB. Chúng tôi chỉ hiển thị thông báo lỗi NGHIÊM TRỌNG

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
7

Một lớp

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
33 được sử dụng để kết nối với máy chủ MongoDB. Nó được tạo bằng lệnh gọi phương thức
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
34. 27017 là cổng mặc định mà máy chủ MongoDB lắng nghe

    org.mongodb
    mongo-java-driver
    x.y.z

0

Với phương thức

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
35, chúng tôi truy xuất cơ sở dữ liệu

    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


8

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
00

Phương thức

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
37 tìm tất cả các bộ sưu tập trong cơ sở dữ liệu

    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


8

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
01

Trong cơ sở dữ liệu của chúng tôi, chúng tôi có hai bộ sưu tập này

Ví dụ tiếp theo kết nối với cơ sở dữ liệu


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


8 và lấy số liệu thống kê của nó

com/zetcode/MongoCommand. java

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
02

Ví dụ kết nối với cơ sở dữ liệu


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


8 và thực hiện lệnh
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
61. Nó cho thấy một số thống kê cơ sở dữ liệu

Quảng cáo
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
03

Với phương thức

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
62, chúng ta thực hiện lệnh
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
61. Lệnh trả về một
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
64, đại diện cho tài liệu MongoDB dưới dạng bản đồ

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
04

Chúng tôi lặp qua các mục của tài liệu

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
05

Java MongoDB đọc dữ liệu

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
65 được sử dụng để lưu trữ các tài liệu mongo được trả về từ một bộ sưu tập.
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
66 là một con trỏ để lặp qua các kết quả của một truy vấn cơ sở dữ liệu. Nó đảm bảo rằng nó được đóng lại trong trường hợp có ngoại lệ

quảng cáo

com/zetcode/MongoReadAll. java

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
06

Trong ví dụ, chúng tôi lặp lại tất cả dữ liệu của bộ sưu tập


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


9

Quảng cáo
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
07

Chúng tôi truy xuất bộ sưu tập


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


9 bằng phương pháp
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
69

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
08

Chúng tôi lặp qua các tài liệu của bộ sưu tập. Phương pháp

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
70 tìm tất cả các tài liệu trong bộ sưu tập

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
09

Toán tử truy vấn Java MongoDB

Có thể lọc dữ liệu bằng các toán tử truy vấn MongoDB, chẳng hạn như

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
71,
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
72 hoặc
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
73. Các toán tử truy vấn có thể được chỉ định trong lớp
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
74

com/zetcode/MongoReadGreaterThan. java

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
30

Ví dụ in tất cả các tài liệu có giá ô tô' lớn hơn 30.000

Quảng cáo Adsbygoogle = cửa sổ. quảng cáo của Google. []). đẩy({});____131

Chúng tôi sử dụng toán tử truy vấn

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
71

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
32

Phương thức

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
76 là một cách viết cú pháp để mã ứng dụng không cần phải lo lắng về việc phải đóng con trỏ theo cách thủ công. Dữ liệu được in ở định dạng JSON bằng phương pháp
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
77

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
33

Đây là đầu ra của ví dụ ở định dạng JSON. Chỉ bao gồm những chiếc xe đắt hơn 30.000

Các phương thức truy vấn bộ lọc nhà máy Java MongoDB

Trình điều khiển Java MongoDB chứa các phương thức xuất xưởng cho các bộ lọc truy vấn

com/zetcode/MongoFilter. java

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
34

Trong ví dụ, chúng tôi truy xuất những chiếc ô tô có giá rơi vào khoảng từ 20.000 đến 50.000

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
35

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
78,
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
79 và
    org.mongodb
    mongo-java-driver
    x.y.z

00 là các phương pháp lọc của nhà máy. Ngoài ra, dữ liệu được sắp xếp theo phương pháp
    org.mongodb
    mongo-java-driver
    x.y.z

01

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
36

Các phép chiếu Java MongoDB

Lớp

    org.mongodb
    mongo-java-driver
    x.y.z

02 cung cấp các phương thức xuất xưởng tĩnh cho tất cả các toán tử chiếu MongoDB. Theo mặc định, tất cả các trường của mỗi tài liệu được chiếu. Chúng tôi có thể sử dụng các phương pháp
    org.mongodb
    mongo-java-driver
    x.y.z

03 và
    org.mongodb
    mongo-java-driver
    x.y.z

04 để xác định trường nào sẽ được chiếu vào đầu ra của chúng tôi

com/zetcode/MongoProjection. java

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
37

Ví dụ loại trừ trường

    org.mongodb
    mongo-java-driver
    x.y.z

05 khỏi đầu ra

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
38

Phương thức

    org.mongodb
    mongo-java-driver
    x.y.z

06 thiết lập một tài liệu mô tả các trường để trả về cho tất cả các tài liệu phù hợp.
    org.mongodb
    mongo-java-driver
    x.y.z

07 là một từ đồng nghĩa với
    org.mongodb
    mongo-java-driver
    x.y.z

08

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
39

Xuất dữ liệu giới hạn Java MongoDB

Tùy chọn truy vấn

    org.mongodb
    mongo-java-driver
    x.y.z

09 chỉ định số lượng tài liệu được trả về và tùy chọn
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
000 bỏ qua số lượng tài liệu được chỉ định

com/zetcode/MongoSkipLimit. java

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
70

Ví dụ đọc từ bộ sưu tập

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
001, bỏ qua hai tài liệu đầu tiên và giới hạn đầu ra ở năm tài liệu

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
71

Phương thức

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
002
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
000 bỏ qua hai tài liệu đầu tiên và phương thức
    org.mongodb
    mongo-java-driver
    x.y.z

09 giới hạn đầu ra ở năm tài liệu

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
72

Ở đây chúng tôi sử dụng cấu trúc Java 8 để in tài liệu

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
73

Java MongoDB tạo bộ sưu tập

Phương thức

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
005
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
006 tạo một bộ sưu tập mới trong cơ sở dữ liệu. Phương pháp
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
007
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
008 chèn một hoặc nhiều tài liệu vào bộ sưu tập

com/zetcode/MongoCreateCollection. java

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
74

Ví dụ tạo một bộ sưu tập


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


9 và chèn chín tài liệu vào đó

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
75

Một bộ sưu tập mới được tạo bằng phương pháp

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
006. Nếu bộ sưu tập đã tồn tại, nó sẽ bị loại bỏ

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
07

Một

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
65 tài liệu được tạo bằng phương pháp
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
69

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
77

Một

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
64 mới được tạo. Nó chứa thông tin về chiếc xe—ID, tên và giá của nó

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
78

Các tài liệu được ghi vào bộ sưu tập bằng phương pháp

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
008

Lớp

    org.mongodb
    mongo-java-driver
    x.y.z

7 có các phương thức phân tích tài liệu JSON. JSON (Ký hiệu đối tượng JavaScript) là một định dạng trao đổi dữ liệu nhẹ. Thật dễ dàng cho con người để đọc và viết

com/zetcode/MongoCollectionFromJSON. java

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
79

Ví dụ tạo bộ sưu tập

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
016 từ dữ liệu JSON

Quảng cáo
    org.mongodb
    mongo-java-driver
    x.y.z

0

Dữ liệu JSON được phân tích cú pháp bằng phương thức

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
017

    org.mongodb
    mongo-java-driver
    x.y.z

1

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
74 được chuyển đến
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
64 và chèn vào bộ sưu tập bằng phương pháp
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
020

    org.mongodb
    mongo-java-driver
    x.y.z

2

Chúng tôi hiển thị bộ sưu tập đã tạo với


    4.0.0

    com.zetcode
    mongocommand
    1.0-SNAPSHOT
    jar

    
        UTF-8
        11
        11
    

    
        
            org.mongodb
            mongo-java-driver
            3.10.2
        
    


7

Java MongoDB sửa đổi tài liệu

Phương pháp

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
007
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
023 được sử dụng để xóa tài liệu và
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
024 để cập nhật tài liệu

com/zetcode/MongoModify. java

Quảng cáo
    org.mongodb
    mongo-java-driver
    x.y.z

3

Ví dụ xóa tài liệu chứa Skoda và cập nhật giá xe Audi

    org.mongodb
    mongo-java-driver
    x.y.z

4

private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
023 xóa tài liệu của
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
026.
private static void selectAllRecordsFromACollection(DBCollection collection) 
{
    DBCursor cursor = collection.find();
    while(cursor.hasNext())
    {
        System.out.println(cursor.next());
    }
}
027 tạo bộ lọc khớp với tất cả các tài liệu trong đó giá trị của tên trường bằng với giá trị đã chỉ định

Làm cách nào để sử dụng BasicDBObject trong Java?

Một DBObject có thể được tạo như sau, sử dụng lớp này. DBObject obj = new BasicDBObject(); . đặt( "foo", "bar" );

BasicDBObject trong Java là gì?

BasicDBObject có thể được sử dụng để tạo hoặc thể hiện tài liệu trong cơ sở dữ liệu MongoDB . DBObject thực sự là một cấu trúc giống như bản đồ với các cặp khóa-giá trị. Nếu bạn tra cứu hệ thống phân cấp lớp, bạn thực sự có thể thấy rằng một BasicDBObject được kế thừa từ java. sử dụng. lớp LinkedHashMap.

Làm cách nào để viết truy vấn bằng Java cho MongoDB?

Cách truy vấn MongoDB® bằng Java .
Tổng quan. .
Cấu hình JDBC từng bước. .
Tải xuống trình điều khiển Java MongoDB. .
Đặt Driver trong Classpath. .
Thiết lập kết nối. .
Nhận cơ sở dữ liệu. .
Nhận bộ sưu tập. .
Nhận một tài liệu duy nhất

Tôi có thể sử dụng MongoDB với Java không?

Giả sử bạn đã cài đặt Java trên hệ thống của mình và IDE yêu thích của bạn đã được kích hoạt và sẵn sàng hoạt động, điều đầu tiên bạn muốn làm là tải MongoDB . Hầu hết các IDE sẽ nhận các tài nguyên được định cấu hình trong Gradle hoặc Maven, một trong hai tài nguyên này có thể được sử dụng để cài đặt trình điều khiển. . Most IDEs will pick up resources configured in Gradle or Maven, either of which can be used to install the driver.