Phương thức remove() trong MongoDB

Phương thức remove() trong MongoDB được sử dụng để xóa Document từ Collection. Phương thức remove() nhận hai tham số. Tham số đầu tiên deletion criteria xác định Document để xóa, và tham số thứ hai là justOne.

  1. deletion criteria : (Tùy ý) Xác định Document để xóa.

  2. justOne : (Tùy ý) Nếu được thiết lập là true hoặc 1, thì chỉ xóa một Document.

Cú pháp

Cú pháp cơ bản của phương thức remove() là như sau:

>db.COLLECTION_NAME.remove(DELLETION_CRITTERIA)

Ví dụ

Bạn theo dõi Collection có tên mycol có dữ liệu sau:

{ "_id" : ObjectId(5983548781331adf45ec5), "title":"MongoDB Overview"}

{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}

{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}

Ví dụ sau sẽ xóa tất cả Document có title là 'MongoDB Overview':

>db.mycol.remove({'title':'MongoDB Overview'})

>db.mycol.find()

{ "_id" : ObjectId(5983548781331adf45ec6), "title":"NoSQL Overview"}

{ "_id" : ObjectId(5983548781331adf45ec7), "title":"Tutorials Point Overview"}

>

Last updated