Table of Contents
回复内容:
Home Backend Development PHP Tutorial Mongodb 根据子文档的某个特定字段排序的问题

Mongodb 根据子文档的某个特定字段排序的问题

Jun 06, 2016 pm 08:28 PM
mongodb php

<code>/* 1 */
{
    "_id" : ObjectId("55e542cd7f8b9aca2b8b4568"),
    "game_id" : 1103,
    "tags" : [ 
        {
            "tag_id" : 10,
            "name" : "西游",
            "rank" : 10,
        }, 
        {
            "tag_id" : 123,
            "name" : "网游",
            "rank" : 1,
        }, 
    ],
    "time_sort" : 1435306
}

/* 2 */
{
    "_id" : ObjectId("55dd8fe67f8b9a25528b64f7"),
    "game_id" : 6408,
    "tags" : [ 
        {
            "tag_id" : 28,
            "name" : "白领最爱",
            "rank" : 20,
        }, 
        {
            "tag_id" : 123,
            "name" : "网游",
            "rank" : 2,
        }, 
    ],
    "time_sort" : 1411574
}

/* 3 */
{
    "_id" : ObjectId("55dd91197f8b9a25528b9585"),
    "game_id" : 18498,
    "tags" : [ 
        {
            "tag_id" : 19,
            "name" : "回合制",
            "pc_rank" : 120,
        }, 
        {
            "tag_id" : 123,
            "name" : "网游",
            "rank" : 3,
        }, 
    ],
    "time_sort" : 1437385
}</code>
Copy after login
Copy after login

现在只想通过 网游标签的rank排序,语句应该怎么写

回复内容:

<code>/* 1 */
{
    "_id" : ObjectId("55e542cd7f8b9aca2b8b4568"),
    "game_id" : 1103,
    "tags" : [ 
        {
            "tag_id" : 10,
            "name" : "西游",
            "rank" : 10,
        }, 
        {
            "tag_id" : 123,
            "name" : "网游",
            "rank" : 1,
        }, 
    ],
    "time_sort" : 1435306
}

/* 2 */
{
    "_id" : ObjectId("55dd8fe67f8b9a25528b64f7"),
    "game_id" : 6408,
    "tags" : [ 
        {
            "tag_id" : 28,
            "name" : "白领最爱",
            "rank" : 20,
        }, 
        {
            "tag_id" : 123,
            "name" : "网游",
            "rank" : 2,
        }, 
    ],
    "time_sort" : 1411574
}

/* 3 */
{
    "_id" : ObjectId("55dd91197f8b9a25528b9585"),
    "game_id" : 18498,
    "tags" : [ 
        {
            "tag_id" : 19,
            "name" : "回合制",
            "pc_rank" : 120,
        }, 
        {
            "tag_id" : 123,
            "name" : "网游",
            "rank" : 3,
        }, 
    ],
    "time_sort" : 1437385
}</code>
Copy after login
Copy after login

现在只想通过 网游标签的rank排序,语句应该怎么写

管道查询然后用unwind打撒tags,再排序

<code>"tags" : [ 
        {
            "tag_id" : 28,
            "name" : "白领最爱",
            "rank" : 20,
        }, 
        {
            "tag_id" : 123,
            "name" : "网游",
            "rank" : 2,
        }, 
    ],</code>
Copy after login

tags数组里面的两个元素都有rank字段,你要根据第一个rank排序还是第二个rank排序?

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) How can you prevent a class from being extended or a method from being overridden in PHP? (final keyword) Apr 08, 2025 am 12:03 AM

In PHP, the final keyword is used to prevent classes from being inherited and methods being overwritten. 1) When marking the class as final, the class cannot be inherited. 2) When marking the method as final, the method cannot be rewritten by the subclass. Using final keywords ensures the stability and security of your code.

The Future of PHP: Adaptations and Innovations The Future of PHP: Adaptations and Innovations Apr 11, 2025 am 12:01 AM

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP: Is It Dying or Simply Adapting? PHP: Is It Dying or Simply Adapting? Apr 11, 2025 am 12:13 AM

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

Navicat's method to view MongoDB database password Navicat's method to view MongoDB database password Apr 08, 2025 pm 09:39 PM

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

H5: Tools, Frameworks, and Best Practices H5: Tools, Frameworks, and Best Practices Apr 11, 2025 am 12:11 AM

The tools and frameworks that need to be mastered in H5 development include Vue.js, React and Webpack. 1.Vue.js is suitable for building user interfaces and supports component development. 2.React optimizes page rendering through virtual DOM, suitable for complex applications. 3.Webpack is used for module packaging and optimize resource loading.

Composer Expertise: What Makes Someone Skilled Composer Expertise: What Makes Someone Skilled Apr 11, 2025 pm 12:41 PM

To become proficient when using Composer, you need to master the following skills: 1. Proficient in using composer.json and composer.lock files, 2. Understand how Composer works, 3. Master Composer's command line tools, 4. Understand basic and advanced usage, 5. Familiar with common errors and debugging techniques, 6. Optimize usage and follow best practices.

PHP's Current Status: A Look at Web Development Trends PHP's Current Status: A Look at Web Development Trends Apr 13, 2025 am 12:20 AM

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

See all articles