Cơ BảnMongoDB iconMongoDB

`findOne`, `find`, `insertOne`, `updateOne` trong MongoDB dùng thế nào?

Các thao tác CRUD cơ bản làm việc với collection: findOne lấy một document, find trả cursor, insertOne thêm document, updateOne cập nhật document match đầu tiên.

Ví dụ:

javascript
await users.insertOne({ email: "a@example.com", name: "Ada" })
await users.updateOne({ email: "a@example.com" }, { $set: { name: "Ada Lovelace" } })
const user = await users.findOne({ email: "a@example.com" })

Cần luôn hiểu filter nào đang dùng index, nếu không collection scan sẽ làm chậm khi dữ liệu lớn.

Xem toàn bộ MongoDB cùng filter theo level & chủ đề con.

Mở danh sách MongoDB