MongoDB advanced Query [aggregation]

Source: Internet
Author: User
Tags mongodb version
I am familiar with MongoDB because I recently tested MongoDB. MongoDB is still simple, and I have mastered a lot of content in just one week. This is indeed easier than other databases.

I am familiar with MongoDB because I recently tested MongoDB. MongoDB is still simple, and I have mastered a lot of content in just one week. This is indeed easier than other databases.

I am familiar with MongoDB because I recently tested MongoDB. MongoDB is still simple, and I have mastered a lot of content in just one week. It is much easier than other databases.

I installed:

MongoDB version 2.0.8

System: 64-bit Ubuntu 12.04

Let's show him my table structure first. [Oh sorry, Mongo is called a set]

As you can see, I try my best to simulate real-life scenarios. This is a person's entity. He has a basic manId, manName, friend [myFriends], a favorite fruit [fruits], and each fruit has a favorite weight.

What's bad is that you still see a "_ class" field? Because I am a Java developer and I also like to use Spring, I chose the Spring Data Mongo Class Library [which is also a framework, but I don't think so].

Many people are getting bored with Spring. Yes, Spring is ambitious. He almost wants to monopolize everything in Java. There is no way I can't do without Spring, so that I don't need to learn any other frameworks. I have learned a lot about Spring, such as Spring Security, Spring Integration, and Spring Batch... Without inventing the wheel, he has already provided many programming scenarios. I have used those scenarios to solve many problems in my work and make my work very efficient. So I learned more about it. Spring Data Mongo encapsulates the mongodb java driver and provides the same programming style as SpringJDBC/Template.

See:

Let's go directly to MongoDB.

  • Max and Min
  • When I was testing Mongo, my colleague and I still wrote less than half of the index. He wanted to query the maximum value of a field. After finding the result for half a day, the document did not find the function about max. I am also wondering if this is a regular function. Why not? Later, I checked the information and confirmed that Mongo does not provide direct max and min functions. However, this can be achieved through indirect [sort and limit.

    To query the maximum value, we only need to sort the result set in descending order and take the first value.

    For example, in my example, I want to obtain the largest person of age in the collection.

    Db. person. find ({}). sort ({"age":-1}). limit (1)

    If you want the youngest person, you only need to change sort to {"age": 1.

    Of course, we use sort, Which is okay for a small number of documents. If you need to create an index for age for a large amount of data, otherwise this operation will take a long time.

    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.