You need to connect to a MongoDB server, create a new database named 'library', create a collection named 'books' within the 'library' database, and list all databases on the server. Arrange positions in order:
Các phương án trả lời
Chọn một phương ánLời giảiChọn phương án để mở
Đáp án đúng: "Step 1 is to create a
new database reference using client.db('library') Step 2 is to create a new collection using await db.createCollection('books');Step 3 is to list all databases using await adminDb.listDatabases(); ". Sau khi client.connect() xong, client.db('library') chỉ tạo một tham chiếu tới cơ sở dữ liệu, MongoDB tạo nó thật khi có collection hoặc dữ liệu đầu tiên; vì vậy phải có tham chiếu db trước thì mới gọi được db.createCollection('books'), và lệnh này làm library thực sự xuất hiện trên máy chủ. Cuối cùng adminDb, lấy từ client.db().admin(), gọi listDatabases() thì danh sách đã có library. Thứ tự mở đầu bằng "Step 1 is to create a new collection" sai vì biến db chưa tồn tại lúc gọi createCollection. Thứ tự "Step 1 is to list all databases using await adminDb.listDatabases(); Step 2 is to create a new collection" vừa liệt kê quá sớm khi library chưa có trong danh sách, vừa tạo collection trước khi có tham chiếu db. Thứ tự "Step 1 is to list all databases using await adminDb.listDatabases(); Step 2 is to create a new database reference" tạo đúng trình tự nhưng liệt kê trước nên không thấy library vừa tạo, trái yêu cầu.