MongoDB operation manual CRUD Delete remove, mongodbcrud

Source: Internet
Author: User

MongoDB operation manual CRUD Delete remove, mongodbcrud
Delete record overview in MongoDB, the db. collection. remove () method is used to delete records in the set. You can delete all records, delete all qualified records, or delete only one record.
Delete all records delete all records in a set. You only need to pass an empty query object {} to the remove () method. The remove () method does not delete the index.
Example: db. testData. remove ({});
Using the remove () method to delete all records in a set may be more efficient than using the drop () method to delete the entire set containing indexes, and then recreating the set and index.
Delete a record that meets the condition to delete all the records that meet the condition in a set. Pass the remove () method to a query object.
For example, db. testData. remove ({type: 'food'}); // records whose type field is "food" will be deleted.
For large-scale delete operations, if you want to copy some records to a new set and then use the drop () method to delete the entire set, the remove () operation may be more efficient.
Delete a single qualified record to delete a qualified record in a set. A justOne parameter must be set to true or 1 for the remove () method.
Example: db. testData. remove ({type: 'food'}, 1 );
To delete a record in a certain order in the Set, use the findAndModify () method.
Why does MongoDB replace MySQL?

MongoDB is a document-oriented database, which is currently developed and maintained by 10gen. It has rich functions and is complete and can completely replace MySQL. In the process of using MongoDB as a product prototype, we summarized some highlights of MonogDB: The JSON-style syntax is easy to understand and understand: mongoDB uses the JSON variant BSON as the internal storage format and syntax. All operations on MongoDB use JSON-style syntax, and the data submitted or received by the client is displayed in JSON format. Compared with SQL, it is more intuitive and easy to understand and master. Schema-less: supports embedding sub-documents: MongoDB is a Schema-free document database. A database can have multiple collections, each of which is a Collection of Documents. The Table and Row of Collection and Document are not equal to those of traditional databases. Collection can be created at any time without prior definition. Collection can contain document records with different schemas. This means that the document in your previous record has three attributes, and the document in the next record can have 10 attributes, attribute types can be basic data types (such as numbers, strings, dates, etc.), arrays or hash, or even a sub-document (embed document ). In this way, you can implement the denormalizing data model to improve the query speed. Figure 1 MongoDB is a Schema-free document database. Figure 2 is an example. Works and comments can be designed as a collection. comments are embedded as sub-documents in the comments attribute of art, comments are embedded in the replies attribute as subdocuments of comment sub-documents. According to this design pattern, you only need to retrieve all the relevant information by file id. In MongoDB, we do not emphasize that data must be Normalize. In many cases, we recommend De-normalize. developers can discard the limitations of various paradigms of traditional relational databases, you do not need to map all objects to a Collection. You only need to define the top class. The document model of MongoDB allows us to easily map our own objects to collections for storage. Figure 2 MongoDB supports the simple and easy-to-use query method for Embedded sub-documents: the query in MongoDB is quite comfortable, and the JSON is directly used without the SQL-hard-to-remember syntax, which is quite intuitive. For different development languages, you can use its most basic array or hash format for query. With the added operator, MongoDB supports range query, regular expression query, and subdocument attribute query, which can replace the SQL query of most previous tasks. CRUD is simpler and supports in-place update: you only need to define an array and the insert/update method passed to MongoDB can be automatically inserted or updated. For the update mode, mongoDB supports an upsert option, that is, "If a record exists, it will be updated; otherwise, it will be inserted ". MongoDB's update method also supports Modifier, which allows immediate updates on the server end, saving communication between the client and the server end. These modifer allows MongoDB to have a function similar to Redis, Memcached, and other KV features: Compared with MySQL, MonoDB is simpler and faster.

Why does MongoDB replace MySQL?

MongoDB is a document-oriented database, which is currently developed and maintained by 10gen. It has rich functions and is complete and can completely replace MySQL. In the process of using MongoDB as a product prototype, we summarized some highlights of MonogDB: The JSON-style syntax is easy to understand and understand: mongoDB uses the JSON variant BSON as the internal storage format and syntax. All operations on MongoDB use JSON-style syntax, and the data submitted or received by the client is displayed in JSON format. Compared with SQL, it is more intuitive and easy to understand and master. Schema-less: supports embedding sub-documents: MongoDB is a Schema-free document database. A database can have multiple collections, each of which is a Collection of Documents. The Table and Row of Collection and Document are not equal to those of traditional databases. Collection can be created at any time without prior definition. Collection can contain document records with different schemas. This means that the document in your previous record has three attributes, and the document in the next record can have 10 attributes, attribute types can be basic data types (such as numbers, strings, dates, etc.), arrays or hash, or even a sub-document (embed document ). In this way, you can implement the denormalizing data model to improve the query speed. Figure 1 MongoDB is a Schema-free document database. Figure 2 is an example. Works and comments can be designed as a collection. comments are embedded as sub-documents in the comments attribute of art, comments are embedded in the replies attribute as subdocuments of comment sub-documents. According to this design pattern, you only need to retrieve all the relevant information by file id. In MongoDB, we do not emphasize that data must be Normalize. In many cases, we recommend De-normalize. developers can discard the limitations of various paradigms of traditional relational databases, you do not need to map all objects to a Collection. You only need to define the top class. The document model of MongoDB allows us to easily map our own objects to collections for storage. Figure 2 MongoDB supports the simple and easy-to-use query method for Embedded sub-documents: the query in MongoDB is quite comfortable, and the JSON is directly used without the SQL-hard-to-remember syntax, which is quite intuitive. For different development languages, you can use its most basic array or hash format for query. With the added operator, MongoDB supports range query, regular expression query, and subdocument attribute query, which can replace the SQL query of most previous tasks. CRUD is simpler and supports in-place update: you only need to define an array and the insert/update method passed to MongoDB can be automatically inserted or updated. For the update mode, mongoDB supports an upsert option, that is, "If a record exists, it will be updated; otherwise, it will be inserted ". MongoDB's update method also supports Modifier, which allows immediate updates on the server end, saving communication between the client and the server end. These modifer allows MongoDB to have a function similar to Redis, Memcached, and other KV features: Compared with MySQL, MonoDB is simpler and faster.

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.