MongoDB self-study notes 11---4.3 delete documents

Source: Internet
Author: User
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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.