MongoDB Quick Start Notes (vi) MongoDB Delete a document operation _mongodb

Source: Internet
Author: User
Tags mongodb

MongoDB is a cross-platform, document-oriented database that delivers high performance, high availability, and ease of scaling. MongoDB is a concept that works on collections and documents.

A document is a set of key-value pairs. The document has dynamic mode. Dynamic mode means that a file in the same collection does not have to have a document field or structure with the same set of collections, and that the same fields can hold different types of data.

Db. Collection name. Remove ({query}, Justone)

Query: Filter conditions, optional

Justone: Delete Only the first data that is queried, the value is true or 1 o'clock, only one data is deleted, the default is false, optional.

Prepare the data: Turn the age of _id 1 and 2 into 28

> db.student.update ({_id:1},{$set: {age:28}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> db.student.update ({_id:2},{$set: {age:28}})
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.student.find ()
{"_id": 1, "name": "Zhangsan", "Age": 28}
{"_id": 2, "name": "Lisi", "Age": 28}
{"_id": 3, "name": "Wangwu", "Age": 30}
{"_id": 4, "name": "Zhaoliu", "Age": 28}
{"_id": 5, "name": "Qianliu", "Age": 33}
{"_id": 6, "name": "Sunba", "Age": 32}
{"_id": 7, "name": "Songjiu", "Skill": ["MongoDB", "Java"]}

1. Use two parameters:

Delete the first piece of data from age=28

> Db.student.remove ({age:28}, True)
Writeresult ({"nremoved": 1})
> Db.student.find ()
{"_id": 2, "n Ame ": Lisi", "age":
{"_id": 3, "name": "Wangwu", "age":
{"_id": 4, "name": "Zhaoliu", "Age":
{"_id": 5, "name": "Qianliu", "Age":
32} {"_id": 6, "name": "Sunba", "Age"

2. Use one parameter:

Delete all data from age=28

> Db.student.remove ({age:28})
Writeresult ({"Nremoved": 2})
> Db.student.find ()
{"_id": 3, " Name ': ' Wangwu ', ' age ':
{' _id ': 5, ' name ': ' Qianliu ', ' age ': +}
{' _id ': 6, ' name ': ' Sunba ', ' age ": 32}

3, delete all the data of the collection, "{}" in parentheses must have, to indicate an empty filter condition:

> Db.student.remove ({})

In addition, when the Remove () method is used to delete the data, the table will still exist. Using the Drop () method deletes the table, and the drop () is more efficient than remove ().

Related Article

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.