MongoDB New and query data (i)

Source: Internet
Author: User
Tags findone

New action
The Insert function adds a document to the collection. For example, we want to register a person's information, first we create a local variable in the shell force person, which records the name and gender of people, we want to set persons by Db.persons.insert to insert a record, Query the record you just inserted by Db.persons.findOne ().


Figure 7. New operation
There are several points to note when inserting:
1. Since MongoDB is modeless, do not pre-define and create a collection that will be created automatically the first time you insert it.
2. The document can store any structure of data, of course, in the actual application we store the same type of document.
3. Each time the data is inserted, MongoDB automatically generates this field and value for the document without the "_id" field.


Figure 8. A collection can host different documents


Query operations
MongoDB uses find to query, and the first parameter of find determines which documents are returned by the query, in the form of a document that describes the details of the query to be executed. The empty query document {} will match the entire contents of the collection. Find () returns all the documents in the collection. If you need a query to get the first piece of data you can use FindOne ();

Figure 9: Querying the collection using Find and FindOne

Use Find () to return a cursor object, but the shell will automatically traverse the cursor to return an initialized set and allow us to continue using an iterative output. But we can also use cursors directly to output.

                                                                                  Figure 10. Using it to traverse query results

can also traverse the query results through cursors and while.

                                                                Figure 11. Using Cursors to view query results
Sometimes we don't need to query all the key-value pairs of a collection, At this point we can use the second parameter of FindOne or find to specify the key that you want to return. This saves both the transmitted traffic and the time and memory consumption of the client decoding the document;


Figure 12. Query results return the specified field
The values in the query criteria must be constants and cannot refer to the values of other keys in the document.


Figure 13: You cannot reference another field as a value in a query condition
MongoDB provides comparison operators, $LT (<), $lte (<=), $GT (>), $gte (>=), $ne (! =)


Figure 14: Comparison operators in queries
There are two ways to make or queries in MongoDB, $in can be used to query a record with a key equal to multiple values, which corresponds to $nin.


Figure 15. Or in the query

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.