MongoDB operation manual CRUD query pointer, mongodbcrud

Source: Internet
Author: User

MongoDB operation manual CRUD query pointer, mongodbcrud
Enumeration traversal pointer Overview As mentioned earlier, if db. collection. find () is not specified to a variable declared by var, the first 20 records will be automatically enumerated.
Manual enumeration pointer in the mongo console, assign the query to a variable declared by var, so that it does not automatically enumerate.
Var cur = db. testData. find ();
Then, every time this pointer is called, 20 entries will be automatically traversed.
Cur;
You can also use the next () method of the pointer to obtain the next record.
Var cur = db. testData. find ();
While (cur. hasNext ())
{
Print (tojson (cur. next (); // The print operation here can be replaced by printjson (cur. next ());
}
You can use the forEach () method of the pointer to traverse the pointer data:
Var cur = db. testData. find ();
Cur. forEach (printjson );
The enumerated subscript is in the mongo console. You can use the toArray () method to access the pointer results.
Var cur = db. testData. find ();
Var arr = cur. toArray ();
Var item = arr [2];
The toArray () method loads all query results to the memory. This method traverses the entire pointer.
In addition, some drivers provide the method to directly use the array subscript, which calls the abbreviation of toArray.
Var cur = db. testData. find ();
Var item = cur [3];
The above two sentences are equivalent to cur. toArray () [3];


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.