4.3 Delete a document in mongodb. If you need to delete one or more documents in the mongodb collection, you must use the remove () method. The syntax of this method is as follows: db. [set name]. remove (query, justOne) parameter query represents the query condition of the deleted document. This parameter can be used in the query
4.3 Delete a document in mongodb. If you need to delete one or more documents in the mongodb collection, you must use the remove () method. The syntax of this method is as follows: db. [set name]. remove (query, justOne) parameter query represents the query condition of the deleted document. This parameter can be used in the query
4.3 Delete a document
In mongodb, if we need to delete one or more data entries in the mongodb collection, that is, to delete one or more documents, we need to use the remove () method. The method syntax is as follows:
Db. [set name]. remove ( , )
The query parameter indicates the query condition of the deleted document. This parameter can be used by all the methods we learned in the query. The parameter justOne indicates whether to delete only one document. The default value is false, indicating that all documents meeting the conditions are deleted. If both parameters are omitted, all documents in the set will be deleted.
{"_ Id": 2, "ary": [2, 3]}
> Db. user. find ()
{"_ Id": 1, "name": "user1", "age": 1}
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "sex": "nan "}
> Db. user. remove ({name:/user */I}, 1)
> Db. user. find ()
{"_ Id": 2, "name": "user2", "age": 2}
{"_ Id": 3, "name": "user3", "age": 3}
{"_ Id": 4, "name": "user4", "age": 4}
{"_ Id": 5, "name": "user5", "age": 5}
{"_ Id": 6, "sex": "nan "}
If we directly call the remove () method to delete all elements in the Set, the set will not be deleted. The set will be deleted only when the drop () method of the set is called. For example, if there is no data in the user set, we can use the following code to delete the set.
Db. user. drop ()