MongoDB simple query

Source: Internet
Author: User

I have been idle for a while recently, and I have been able to spend enough time studying code integration. Because there are too many linguistics, I gradually feel that it is meaningless. I learned a little from other groups using MongoDB. In view of this quick start, because the foundation is not very reliable, it will always be forgotten. Looking at the MongoDB authoritative guide, although it is in English, there is no difficulty in reading it. It is probably that this book is indeed easy to understand. I usually read some English documents. It would be nice to stick to it for an hour. If I look at the documents, I will not be sleepy, but some commands cannot be understood immediately. Let's talk about the query first. I think my understanding of the database is limited to adding, deleting, modifying, and querying these operations. As for more in-depth information, no matter which database is used, it has never been deeply touched. After all, it's not DBA. We only need to know how to call the database in a language we are familiar.

I don't have to worry about MongoDB any more. There are many such problems on the Internet. It is said that it is not a relational database, and it often does not feel like a database, so it is closer to some functions, of course, MongoDB does encapsulate many functions.

1. Find

Content is a collection, which is equivalent to a table in a relational database, and DB is fixed. If find does not contain a parameter, it is equivalent

Select * from table

Returns all fields of all records in the data table.

You can query records that meet certain conditions (document ).


The query gender field is male's document, which is equivalent to select * from table where gender = male

Of course, the query conditions can be multiple


I will not explain too much about simple statements. Everyone knows.

All fields are returned, and people are dazzled. If you can select a field of interest, you can. That's of course.


_ Id is automatically added to the database after data is inserted, which is equivalent to the primary key. It must be clear that in addition to the conditions, the find parameter also has a {} parameter. Setting the field value to 1 indicates that this field is to be displayed. _ Id is output even if there is no explicit output. Of course, you can also specify fields that are not output,


Set _ id to 0, indicating that this field is not explicitly declared, so only the fields that we strongly require output are displayed!

2. Use >,>=,<,<= ,! = Corresponding to GT, GTE, LT, LTE, ne respectively

For example, search for the document with age between 10 and 25


Find the document where degree is not bachelor


There are two implementation methods for or. Of course, these two methods correspond to different scenarios.

Use in to determine whether a field belongs to a member of the set,

For example, you can view the document whose age is 11, 15, 22, or 25.


Use Nin to search


Of course, there are limitations when using in to judge. Only one field can be judged. If two fields need to be matched, The in cannot meet the requirements, and the or keyword is required.


MoD is a remainder operation, which is no different from other courses. The first parameter is the divisor, and the second parameter is the remainder.


3. query specific types


This is a document of the content type. We need to query this collection.

Null should be familiar to everyone, that is, null. The post field of a document in content is null. If you want to find this document,


In the above query results, except for the document with the post indeed being null, all those without the post location are also output. This certainly does not meet the requirements. In this case, the exists keyword appears.



MongoDB supports regular expressions,

For example, find the document whose name is lunar or lunar.

Flags (I) is case-insensitive.

We can look for strings, which are no different from common regular expressions.


Search for Arrays

First look at the collection structure


If the string you want to search for matches more than one field, you can use $ all.


Array matching is usually accurate. In this way, if the order of the content in the array is different, incorrect search results will occur.


The index starts from scratch. The preceding statement indicates that the second element of the array to be queried is apple.

4. Search for nested Structures

Collections used


If you want to find the document named Xin Lau, the following statements are correct.


However, if we suddenly want to add a middle field to this document, the above query statement will immediately become invalid. Because MongoDB exactly matches each field, what should we do? Specify fields explicitly


The above is a little new summary, and it seems that MongoDB is not difficult, but it is still unfamiliar with some of its wonders, and it does not feel like a database, it is more like a powerful function tool. However, you may continue to pay attention to it later.

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.