Chèn đối tượng mongodb java

com. mongodbDBCollectionsave

Javadoc

Cập nhật tài liệu hiện có hoặc chèn tài liệu tùy thuộc vào tham số. Nếu tài liệu không chứa trường '_id', thì phương thức này sẽ thực hiện thao tác chèn với các trường được chỉ định trong tài liệu cũng như trường '_id' với giá trị objectId duy nhất. Nếu tài liệu chứa trường '_id', thì phương thức này sẽ thực hiện truy vấn upsert truy vấn bộ sưu tập trên trường '_id'

  • Nếu một tài liệu không tồn tại với giá trị '_id' được chỉ định, phương thức sẽ thực hiện thao tác chèn với các trường được chỉ định trong tài liệu
  • Nếu một tài liệu tồn tại với giá trị '_id' được chỉ định, thì phương thức này sẽ thực hiện cập nhật, thay thế tất cả trường trong bản ghi hiện có bằng các trường từ tài liệu

Các phương thức phổ biến của DBCollection

  • tìm thấy

    Chọn tài liệu trong bộ sưu tập và đưa con trỏ đến tài liệu đã chọn

  • chèn

    Chèn tài liệu vào bộ sưu tập. Nếu bộ sưu tập không tồn tại trên máy chủ, thì nó sẽ là

  • tìm một

    Nhận một tài liệu từ bộ sưu tập của '_id'

  • gỡ bỏ

    Xóa tài liệu khỏi bộ sưu tập

  • cập nhật
  • đếm

    Nhận số lượng tài liệu trong bộ sưu tập phù hợp với tiêu chí

  • tạo Index

    Buộc tạo chỉ mục tăng dần trên một trường với các tùy chọn mặc định

  • làm rơi

    Bỏ (xóa) bộ sưu tập này khỏi cơ sở dữ liệu. Sử dụng cẩn thận

  • findAndModify

    Sửa đổi nguyên tử và trả lại một tài liệu. Theo mặc định, tài liệu trả về không bao gồm t

  • getName

    Lấy tên của một bộ sưu tập

  • getDB

    Trả về cơ sở dữ liệu mà bộ sưu tập này là thành viên của

  • getIndexInfo

    Trả về danh sách các chỉ mục cho bộ sưu tập này. Mỗi đối tượng trong danh sách là "tài liệu thông tin" từ

  • lấyDB,
  • lấy IndexInfo,
  • đảm bảo Index,
  • tổng hợp,
  • getFullName,
  • riêng biệt,
  • getCount,
  • getStats,
  • thiết lậpViếtMối quan tâm

Phổ biến trong Java

  • Tạo tài liệu JSON từ các lớp java bằng gson
  • getContentResolver (Ngữ cảnh)
  • runOnUiThread (Hoạt động)
  • lịchAtFixedRate (Hẹn giờ)
  • FileOutputStream (java. io)

    Luồng đầu ra ghi byte vào tệp. Nếu tệp đầu ra tồn tại, nó có thể được thay thế hoặc nối thêm

  • Trình đọc tệp (java. io)

    Trình đọc chuyên dụng đọc từ một tệp trong hệ thống tệp. Tất cả các yêu cầu đọc được thực hiện bằng cách gọi cho tôi

  • IOException (java. io)

    Báo hiệu một lỗi chung, liên quan đến I/O. Chi tiết lỗi có thể được chỉ định khi gọi hàm tạo, một

  • Hẹn giờ (java. sử dụng)

    Bộ hẹn giờ lên lịch TimerTask một lần hoặc định kỳ để thực hiện. thích java hơn. sử dụng. đồng thời. Lên kế hoạch

  • Người sưu tầm (java. sử dụng. luồng)
  • Base64 (tổ chức. apache. của chung. giải mã. nhị phân)

    Cung cấp mã hóa và giải mã Base64 theo định nghĩa của RFC 2045. Lớp này thực hiện phần 6. 8. Căn cứ

  • Các plugin Sublime Text hàng đầu

Thực hiện thao tác ghi để chèn tài liệu mới vào bộ sưu tập, cập nhật tài liệu hoặc tài liệu hiện có trong bộ sưu tập, thay thế tài liệu hiện có trong bộ sưu tập hoặc xóa tài liệu hoặc tài liệu hiện có khỏi bộ sưu tập

điều kiện tiên quyết

  • Ví dụ dưới đây yêu cầu một bộ sưu tập

    MongoClient mongoClient = MongoClients.create();
    MongoDatabase database = mongoClient.getDatabase("test");
    MongoCollection collection = database.getCollection("restaurants");
    
    7 trong cơ sở dữ liệu
    MongoClient mongoClient = MongoClients.create();
    MongoDatabase database = mongoClient.getDatabase("test");
    MongoCollection collection = database.getCollection("restaurants");
    
    8. Để tạo và điền vào bộ sưu tập, hãy làm theo hướng dẫn trong github

  • Bao gồm các câu lệnh nhập sau

     import com.mongodb.*;
     import com.mongodb.client.MongoClients;
     import com.mongodb.clientMongoClient;
     import com.mongodb.client.MongoCollection;
     import com.mongodb.client.MongoDatabase;
     import com.mongodb.client.model.Filters;
     import static com.mongodb.client.model.Filters.*;
     import static com.mongodb.client.model.Updates.*;
     import com.mongodb.client.model.UpdateOptions;
     import com.mongodb.client.result.*;
     import org.bson.Document;
     import org.bson.types.ObjectId;
    
     import java.util.List;
     import java.util.Arrays;
     import java.util.ArrayList;
    

Kết nối với Triển khai MongoDB

Kết nối với triển khai MongoDB, khai báo và xác định một phiên bản

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
9

Ví dụ: bao gồm mã sau để kết nối với triển khai MongoDB độc lập chạy trên máy chủ cục bộ trên cổng

Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
0 và xác định
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
1 để tham chiếu cơ sở dữ liệu
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
8 và
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
3 để tham chiếu bộ sưu tập
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
7

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");

Để biết thêm thông tin về cách kết nối với MongoDB, hãy xem Kết nối với MongoDB

Chèn tài liệu mới

Để chèn một tài liệu vào bộ sưu tập, bạn có thể sử dụng phương thức của bộ sưu tập

Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);

Ghi chú

Nếu không có trường

Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 cấp cao nhất nào được chỉ định trong tài liệu, trình điều khiển Java sẽ tự động thêm trường
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 vào tài liệu được chèn

Chèn nhiều tài liệu

Để thêm nhiều tài liệu, bạn có thể sử dụng phương thức của bộ sưu tập, phương thức này lấy danh sách tài liệu để chèn

Ví dụ sau chèn hai tài liệu vào bộ sưu tập

Document doc1 = new Document("name", "Amarcord Pizzeria")
               .append("contact", new Document("phone", "264-555-0193")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.88502, 40.749556)))
               .append("stars", 2)
               .append("categories", Arrays.asList("Pizzeria", "Italian", "Pasta"));


Document doc2 = new Document("name", "Blue Coffee Bar")
               .append("contact", new Document("phone", "604-555-0102")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.97902, 40.8479556)))
               .append("stars", 5)
               .append("categories", Arrays.asList("Coffee", "Pastries"));

List documents = new ArrayList();
documents.add(doc1);
documents.add(doc2);

collection.insertMany(documents);

Ghi chú

Nếu không có trường

Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 cấp cao nhất nào được chỉ định trong tài liệu, trình điều khiển Java sẽ tự động thêm trường
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 vào tài liệu được chèn

Cập nhật tài liệu hiện có

Để cập nhật các tài liệu hiện có trong một bộ sưu tập, bạn có thể sử dụng các phương thức của bộ sưu tập

bộ lọc

Bạn có thể chuyển tài liệu bộ lọc vào các phương thức để chỉ định tài liệu nào sẽ cập nhật. Đặc tả tài liệu bộ lọc giống như đối với hoạt động đọc. Để tạo điều kiện thuận lợi cho việc tạo các đối tượng bộ lọc, trình điều khiển Java cung cấp trình trợ giúp

Document doc1 = new Document("name", "Amarcord Pizzeria")
               .append("contact", new Document("phone", "264-555-0193")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.88502, 40.749556)))
               .append("stars", 2)
               .append("categories", Arrays.asList("Pizzeria", "Italian", "Pasta"));


Document doc2 = new Document("name", "Blue Coffee Bar")
               .append("contact", new Document("phone", "604-555-0102")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.97902, 40.8479556)))
               .append("stars", 5)
               .append("categories", Arrays.asList("Coffee", "Pastries"));

List documents = new ArrayList();
documents.add(doc1);
documents.add(doc2);

collection.insertMany(documents);
3

Để chỉ định một bộ lọc trống (i. e. phù hợp với tất cả các tài liệu trong một bộ sưu tập), sử dụng một đối tượng

Document doc1 = new Document("name", "Amarcord Pizzeria")
               .append("contact", new Document("phone", "264-555-0193")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.88502, 40.749556)))
               .append("stars", 2)
               .append("categories", Arrays.asList("Pizzeria", "Italian", "Pasta"));


Document doc2 = new Document("name", "Blue Coffee Bar")
               .append("contact", new Document("phone", "604-555-0102")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.97902, 40.8479556)))
               .append("stars", 5)
               .append("categories", Arrays.asList("Coffee", "Pastries"));

List documents = new ArrayList();
documents.add(doc1);
documents.add(doc2);

collection.insertMany(documents);
4 trống

Cập nhật toán tử

Để thay đổi một trường trong tài liệu, MongoDB cung cấp các toán tử cập nhật. Để chỉ định sửa đổi cần thực hiện bằng toán tử cập nhật, hãy sử dụng tài liệu cập nhật

Để tạo điều kiện thuận lợi cho việc tạo các tài liệu cập nhật, trình điều khiển Java cung cấp lớp

Document doc1 = new Document("name", "Amarcord Pizzeria")
               .append("contact", new Document("phone", "264-555-0193")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.88502, 40.749556)))
               .append("stars", 2)
               .append("categories", Arrays.asList("Pizzeria", "Italian", "Pasta"));


Document doc2 = new Document("name", "Blue Coffee Bar")
               .append("contact", new Document("phone", "604-555-0102")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.97902, 40.8479556)))
               .append("stars", 5)
               .append("categories", Arrays.asList("Coffee", "Pastries"));

List documents = new ArrayList();
documents.add(doc1);
documents.add(doc2);

collection.insertMany(documents);
5

quan trọng

Trường

Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 là bất biến; . e. bạn không thể thay đổi giá trị của trường
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6

Cập nhật một tài liệu duy nhất

Phương pháp này cập nhật tối đa một tài liệu, ngay cả khi điều kiện bộ lọc khớp với nhiều tài liệu trong bộ sưu tập

Thao tác sau trên bộ sưu tập

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
7 cập nhật tài liệu có trường
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 bằng với
collection.updateOne(
                eq("_id", new ObjectId("57506d62f57802807471dd41")),
                combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
1

collection.updateOne(
                eq("_id", new ObjectId("57506d62f57802807471dd41")),
                combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));

Cụ thể, thao tác sử dụng

  • để đặt giá trị của trường

    collection.updateOne(
                    eq("_id", new ObjectId("57506d62f57802807471dd41")),
                    combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
    
    3 thành
    collection.updateOne(
                    eq("_id", new ObjectId("57506d62f57802807471dd41")),
                    combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
    
    4 và trường
    collection.updateOne(
                    eq("_id", new ObjectId("57506d62f57802807471dd41")),
                    combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
    
    5 thành
    collection.updateOne(
                    eq("_id", new ObjectId("57506d62f57802807471dd41")),
                    combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
    
    6, và

  • để sửa đổi trường

    collection.updateOne(
                    eq("_id", new ObjectId("57506d62f57802807471dd41")),
                    combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
    
    8 thành ngày hiện tại. Nếu trường
    collection.updateOne(
                    eq("_id", new ObjectId("57506d62f57802807471dd41")),
                    combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
    
    8 không tồn tại, người vận hành sẽ thêm trường này vào tài liệu

tiền boa

Trong một số trường hợp bạn có thể cần cập nhật nhiều trường trong tài liệu, việc thay thế tài liệu có thể hiệu quả hơn. Nhìn thấy

Cập nhật nhiều tài liệu

Phương pháp cập nhật tất cả các tài liệu phù hợp với điều kiện lọc

Hoạt động sau đây trên bộ sưu tập

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
7 cập nhật tất cả các tài liệu có trường
collection.updateOne(
                eq("_id", new ObjectId("57506d62f57802807471dd41")),
                combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
3 bằng với
collection.updateMany(
              eq("stars", 2),
              combine(set("stars", 0), currentDate("lastModified")));
3

collection.updateMany(
              eq("stars", 2),
              combine(set("stars", 0), currentDate("lastModified")));

Cụ thể, thao tác sử dụng

  • để đặt giá trị của trường

    collection.updateOne(
                    eq("_id", new ObjectId("57506d62f57802807471dd41")),
                    combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
    
    3 thành
    collection.updateMany(
                  eq("stars", 2),
                  combine(set("stars", 0), currentDate("lastModified")));
    
    6 và

  • để sửa đổi trường

    collection.updateOne(
                    eq("_id", new ObjectId("57506d62f57802807471dd41")),
                    combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
    
    8 thành ngày hiện tại. Nếu trường
    collection.updateOne(
                    eq("_id", new ObjectId("57506d62f57802807471dd41")),
                    combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
    
    8 không tồn tại, người vận hành sẽ thêm trường này vào tài liệu

Tùy chọn cập nhật

Với các phương pháp và, bạn có thể bao gồm một tài liệu

collection.updateOne(
                eq("_id", 1),
                combine(set("name", "Fresh Breads and Tulips"), currentDate("lastModified")),
                new UpdateOptions().upsert(true).bypassDocumentValidation(true));
2 để chỉ định tùy chọn hoặc tùy chọn

collection.updateOne(
                eq("_id", 1),
                combine(set("name", "Fresh Breads and Tulips"), currentDate("lastModified")),
                new UpdateOptions().upsert(true).bypassDocumentValidation(true));

Thay thế một tài liệu hiện có

Để thay thế một tài liệu hiện có trong một bộ sưu tập, bạn có thể sử dụng phương thức của bộ sưu tập

quan trọng

Trường

Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 là bất biến; . e. bạn không thể thay thế giá trị trường
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6

bộ lọc

Bạn có thể chuyển tài liệu bộ lọc sang phương thức để chỉ định tài liệu nào sẽ thay thế. Đặc tả tài liệu bộ lọc giống như đối với hoạt động đọc. Để tạo điều kiện thuận lợi cho việc tạo các đối tượng bộ lọc, trình điều khiển Java cung cấp trình trợ giúp

Document doc1 = new Document("name", "Amarcord Pizzeria")
               .append("contact", new Document("phone", "264-555-0193")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.88502, 40.749556)))
               .append("stars", 2)
               .append("categories", Arrays.asList("Pizzeria", "Italian", "Pasta"));


Document doc2 = new Document("name", "Blue Coffee Bar")
               .append("contact", new Document("phone", "604-555-0102")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.97902, 40.8479556)))
               .append("stars", 5)
               .append("categories", Arrays.asList("Coffee", "Pastries"));

List documents = new ArrayList();
documents.add(doc1);
documents.add(doc2);

collection.insertMany(documents);
3

Để chỉ định một bộ lọc trống (i. e. phù hợp với tất cả các tài liệu trong một bộ sưu tập), sử dụng một đối tượng

Document doc1 = new Document("name", "Amarcord Pizzeria")
               .append("contact", new Document("phone", "264-555-0193")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.88502, 40.749556)))
               .append("stars", 2)
               .append("categories", Arrays.asList("Pizzeria", "Italian", "Pasta"));


Document doc2 = new Document("name", "Blue Coffee Bar")
               .append("contact", new Document("phone", "604-555-0102")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.97902, 40.8479556)))
               .append("stars", 5)
               .append("categories", Arrays.asList("Coffee", "Pastries"));

List documents = new ArrayList();
documents.add(doc1);
documents.add(doc2);

collection.insertMany(documents);
4 trống

Phương pháp này thay thế nhiều nhất một tài liệu, ngay cả khi điều kiện lọc phù hợp với nhiều tài liệu trong bộ sưu tập

Thay thế một tài liệu

Để thay thế một tài liệu, hãy chuyển một tài liệu mới cho phương thức

quan trọng

Tài liệu thay thế có thể có các trường khác với tài liệu gốc. Trong tài liệu thay thế, bạn có thể bỏ qua trường

Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 vì trường
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 là bất biến;

Thao tác sau trên bộ sưu tập

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
7 thay thế tài liệu có trường
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 bằng
collection.updateOne(
                eq("_id", new ObjectId("57506d62f57802807471dd41")),
                combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
1

collection.replaceOne(
                eq("_id", new ObjectId("57506d62f57802807471dd41")),
                new Document("name", "Green Salads Buffet")
                        .append("contact", "TBD")
                        .append("categories", Arrays.asList("Salads", "Health Foods", "Buffet")));

Xem thêm

Tùy chọn cập nhật

Với , bạn có thể bao gồm một tài liệu

collection.updateOne(
                eq("_id", 1),
                combine(set("name", "Fresh Breads and Tulips"), currentDate("lastModified")),
                new UpdateOptions().upsert(true).bypassDocumentValidation(true));
2 để chỉ định tùy chọn hoặc tùy chọn

collection.replaceOne(
                eq("name", "Orange Patisserie and Gelateria"),
                new Document("stars", 5)
                        .append("contact", "TBD")
                        .append("categories", Arrays.asList("Cafe", "Pastries", "Ice Cream")),
                new UpdateOptions().upsert(true).bypassDocumentValidation(true));

Xóa tài liệu

Để xóa tài liệu trong một bộ sưu tập, bạn có thể sử dụng phương thức và

bộ lọc

Bạn có thể chuyển tài liệu bộ lọc vào các phương thức để chỉ định tài liệu nào cần xóa. Đặc tả tài liệu bộ lọc giống như đối với hoạt động đọc. Để tạo điều kiện thuận lợi cho việc tạo các đối tượng bộ lọc, trình điều khiển Java cung cấp trình trợ giúp

Document doc1 = new Document("name", "Amarcord Pizzeria")
               .append("contact", new Document("phone", "264-555-0193")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.88502, 40.749556)))
               .append("stars", 2)
               .append("categories", Arrays.asList("Pizzeria", "Italian", "Pasta"));


Document doc2 = new Document("name", "Blue Coffee Bar")
               .append("contact", new Document("phone", "604-555-0102")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.97902, 40.8479556)))
               .append("stars", 5)
               .append("categories", Arrays.asList("Coffee", "Pastries"));

List documents = new ArrayList();
documents.add(doc1);
documents.add(doc2);

collection.insertMany(documents);
3

Để chỉ định một bộ lọc trống (i. e. phù hợp với tất cả các tài liệu trong một bộ sưu tập), sử dụng một đối tượng

Document doc1 = new Document("name", "Amarcord Pizzeria")
               .append("contact", new Document("phone", "264-555-0193")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.88502, 40.749556)))
               .append("stars", 2)
               .append("categories", Arrays.asList("Pizzeria", "Italian", "Pasta"));


Document doc2 = new Document("name", "Blue Coffee Bar")
               .append("contact", new Document("phone", "604-555-0102")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.97902, 40.8479556)))
               .append("stars", 5)
               .append("categories", Arrays.asList("Coffee", "Pastries"));

List documents = new ArrayList();
documents.add(doc1);
documents.add(doc2);

collection.insertMany(documents);
4 trống

Xóa một tài liệu

Phương pháp này xóa tối đa một tài liệu, ngay cả khi điều kiện lọc phù hợp với nhiều tài liệu trong bộ sưu tập

Thao tác sau đây trên bộ sưu tập

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
7 xóa tài liệu có trường
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
6 bằng
collection.updateOne(
                eq("_id", new ObjectId("57506d62f57802807471dd41")),
                combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
1

collection.deleteOne(eq("_id", new ObjectId("57506d62f57802807471dd41")));

Xóa nhiều tài liệu

Phương pháp xóa tất cả các tài liệu phù hợp với điều kiện lọc

Thao tác sau trên bộ sưu tập

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
7 xóa tất cả các tài liệu có trường
collection.updateOne(
                eq("_id", new ObjectId("57506d62f57802807471dd41")),
                combine(set("stars", 1), set("contact.phone", "228-555-9999"), currentDate("lastModified")));
3 bằng
collection.deleteOne(eq("_id", new ObjectId("57506d62f57802807471dd41")));
4

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
0

Xem thêm Bỏ bộ sưu tập

Viết mối quan tâm

Mối quan tâm ghi mô tả mức độ xác nhận được yêu cầu từ MongoDB cho các thao tác ghi

Các ứng dụng có thể định cấu hình mối quan tâm ghi ở ba cấp độ

  • Trong một

    collection.deleteOne(eq("_id", new ObjectId("57506d62f57802807471dd41")));
    
    5

    • Thông qua
      collection.deleteOne(eq("_id", new ObjectId("57506d62f57802807471dd41")));
      
      6, như trong ví dụ sau
    MongoClient mongoClient = MongoClients.create();
    MongoDatabase database = mongoClient.getDatabase("test");
    MongoCollection collection = database.getCollection("restaurants");
    
    1
    • Thông qua
      collection.deleteOne(eq("_id", new ObjectId("57506d62f57802807471dd41")));
      
      7, như trong ví dụ sau
    MongoClient mongoClient = MongoClients.create();
    MongoDatabase database = mongoClient.getDatabase("test");
    MongoCollection collection = database.getCollection("restaurants");
    
    2
  • Trong một

    MongoClient mongoClient = MongoClients.create();
    MongoDatabase database = mongoClient.getDatabase("test");
    MongoCollection collection = database.getCollection("restaurants");
    
    9 thông qua phương thức của nó, như trong ví dụ sau

    MongoClient mongoClient = MongoClients.create();
    MongoDatabase database = mongoClient.getDatabase("test");
    MongoCollection collection = database.getCollection("restaurants");
    
    3
  • Trong một

    MongoClient mongoClient = MongoClients.create();
    MongoDatabase database = mongoClient.getDatabase("test");
    MongoCollection collection = database.getCollection("restaurants");
    
    00 thông qua phương thức của nó, như trong ví dụ sau

    MongoClient mongoClient = MongoClients.create();
    MongoDatabase database = mongoClient.getDatabase("test");
    MongoCollection collection = database.getCollection("restaurants");
    
    4

Các trường hợp

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
9 và
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
00 là bất biến. Gọi
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
04 trên một phiên bản
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
9 hoặc
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
00 hiện có trả về một phiên bản mới và không ảnh hưởng đến phiên bản mà phương thức được gọi

Ví dụ: trong trường hợp sau đây, đối tượng

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
07 có đa số mối quan tâm ghi trong khi mối quan tâm ghi của
Document document = new Document("name", "Café Con Leche")
               .append("contact", new Document("phone", "228-555-0149")
                                       .append("email", "[email protected]")
                                       .append("location",Arrays.asList(-73.92502, 40.8279556)))
               .append("stars", 3)
               .append("categories", Arrays.asList("Bakery", "Coffee", "Pastries"));

collection.insertOne(document);
3 không bị ảnh hưởng

MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
5

Bạn có thể xây dựng

collection.deleteOne(eq("_id", new ObjectId("57506d62f57802807471dd41")));
6,
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
9 hoặc
MongoClient mongoClient = MongoClients.create();
MongoDatabase database = mongoClient.getDatabase("test");
MongoCollection collection = database.getCollection("restaurants");
00 để bao gồm sự kết hợp của mối quan tâm viết, mối quan tâm đọc và sở thích đọc

Làm cách nào để chèn đối tượng vào MongoDB bằng Java?

Kết nối với cơ sở dữ liệu bằng phương thức getDatabase(). Chuẩn bị tài liệu được chèn. Lấy đối tượng của bộ sưu tập mà bạn muốn chèn tài liệu vào, sử dụng phương thức getCollection(). Gọi phương thức insertOne() bằng cách chuyển tài liệu (đã tạo ở trên) làm tham số

Làm cách nào để chèn đối tượng JSON vào MongoDB bằng Java?

Cách đơn giản nhất để nhập JSON vào MongoDB là chuyển nó thành “tổ chức. bson. Đối tượng tài liệu” trước . Lớp này đại diện cho một tài liệu MongoDB chung không có loại cụ thể.

Làm cách nào để chèn mảng đối tượng trong MongoDB bằng Java?

Trình điều khiển Java MongoDB hỗ trợ cả tài liệu DBObject và BSON. .
insert() – Chèn một tài liệu hoặc nhiều tài liệu vào một bộ sưu tập
insertOne() – Chèn một tài liệu vào một bộ sưu tập
insertMany() – Chèn nhiều tài liệu vào một bộ sưu tập

Làm cách nào chúng ta có thể chèn dữ liệu vào MongoDB?

Để chèn dữ liệu vào bộ sưu tập MongoDB, bạn cần sử dụng phương thức insert() hoặc save() của MongoDB .