A problem that Mongodb sorts according to a specific field in a subdocument

Source: Internet
Author: User
Tags sorts
/* 1 */{"_id": ObjectId ("55e542cd7f8b9aca2b8b4568"), "game_id": 1103, "tags": [{"T ag_id ":", "Name": "West Tour", "Rank": Ten,}, {"tag_id": 123, " Name ":" Online Game "," Rank ": 1,},]," Time_sort ": 1435306}/* 2 */{" _id ": ObjectId (" 55dd8fe67f8b9 A25528b64f7 ")," game_id ": 6408," tags ": [{" tag_id ":" "Name": "White Collar favorites", "Rank": "tag_id": 123, "name": "Online Game", "Rank": 2,}, ], "Time_sort": 1411574}/* 3 */{"_id": ObjectId ("55dd91197f8b9a25528b9585"), "game_id": 18498, "tags" : [{"tag_id": +, "name": "Turn-based", "Pc_rank": 120,}, { "tag_id": 123, "name": "Online Game", "Rank": 3,},], "Time_sort": 1437385} 

Now just want to through the online game label Rank Sort, the statement should be how to write

Reply content:

/* 1 */{"_id": ObjectId ("55e542cd7f8b9aca2b8b4568"), "game_id": 1103, "tags": [{"T ag_id ":", "Name": "West Tour", "Rank": Ten,}, {"tag_id": 123, " Name ":" Online Game "," Rank ": 1,},]," Time_sort ": 1435306}/* 2 */{" _id ": ObjectId (" 55dd8fe67f8b9 A25528b64f7 ")," game_id ": 6408," tags ": [{" tag_id ":" "Name": "White Collar favorites", "Rank": "tag_id": 123, "name": "Online Game", "Rank": 2,}, ], "Time_sort": 1411574}/* 3 */{"_id": ObjectId ("55dd91197f8b9a25528b9585"), "game_id": 18498, "tags" : [{"tag_id": +, "name": "Turn-based", "Pc_rank": 120,}, { "tag_id": 123, "name": "Online Game", "Rank": 3,},], "Time_sort": 1437385} 

Now just want to through the online game label Rank Sort, the statement should be how to write

Pipeline query and then use unwind to play with tags, and then sort

"tags" : [         {            "tag_id" : 28,            "name" : "白领最爱",            "rank" : 20,        },         {            "tag_id" : 123,            "name" : "网游",            "rank" : 2,        },     ],

The two elements inside the tags array have the rank field, do you want to sort by the first rank or the second rank?

  • 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.