排序和限制嵌入文档 MongoDB [英] Sort and Limit Embedded Document MongoDB

查看:36
本文介绍了排序和限制嵌入文档 MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 MongoDB 架构是用嵌入的文档构建的,如下所示:

My MongoDB schema is built with embedded documents like this:

{ 
    "_id" : ObjectId("5f0c64e4dd0a36b93c7deafa"), 
    "name" : "Asd", 
    "email" : "asd@neurondigital.tech", 
    "password" : "$2b$12$66OTK8mSWELMF5YiF9HMUuHEeOVLI61aINjWs1Cmn1699lLJfz/7y", 
    "auto_ml" : true, 
    "notification" : true, 
    "photo" : null, 
    "tariff_id" : null, 
    "city" : null, 
    "sub_district" : null, 
    "village" : null, 
    "latitude" : null, 
    "longitude" : null, 
    "created_at" : ISODate("2020-07-13T20:43:00.871+0000"), 
    "updated_at" : ISODate("2020-07-13T23:08:26.149+0000"), 
    "family_members" : [
        {
            "_id" : ObjectId("5f0c98446f0321f6986755d8"), 
            "name" : "Asd Jr.", 
            "email" : "asd.jr@neurondigital.tech", 
            "password" : "$2b$12$K83ScPPb19dtELJs4tc0He9NffE4f9pr9cvjcnpyNoeAUh60cmQXq", 
            "auto_ml" : true, 
            "notification" : true, 
            "photo" : null, 
            "created_at" : ISODate("2020-07-14T00:22:12.249+0000"), 
            "updated_at" : ISODate("2020-07-14T00:22:12.249+0000")
        }, 
        {
            "_id" : ObjectId("5f0c984b6f0321f6986755d9"), 
            "name" : "Asd Grand Jr.", 
            "email" : "asd.grandjr@neurondigital.tech", 
            "password" : "$2b$12$UXfEUGhHf4Hli9oaViirJut.xWAoIWqac6xEdREJKfXq0OVSdGogu", 
            "auto_ml" : true, 
            "notification" : true, 
            "photo" : null, 
            "created_at" : ISODate("2020-07-14T00:22:19.270+0000"), 
            "updated_at" : ISODate("2020-07-14T00:22:19.270+0000")
        }
    ], 
    "rooms" : [
        {
            "_id" : ObjectId("5f0c98826f0321f6986755da"), 
            "name" : "Ruang Makan", 
            "created_at" : ISODate("2020-07-14T00:23:14.839+0000"), 
            "updated_at" : ISODate("2020-07-14T00:23:14.840+0000"), 
            "devices" : [

            ]
        }, 
        {
            "_id" : ObjectId("5f0c98846f0321f6986755db"), 
            "name" : "Kamar Mandi", 
            "created_at" : ISODate("2020-07-14T00:23:16.823+0000"), 
            "updated_at" : ISODate("2020-07-14T00:23:16.823+0000"), 
            "devices" : [

            ]
        }, 
        {
            "_id" : ObjectId("5f0c98866f0321f6986755dc"), 
            "name" : "Kamar Tidur Utama", 
            "created_at" : ISODate("2020-07-14T00:23:18.310+0000"), 
            "updated_at" : ISODate("2020-07-14T00:23:18.310+0000"), 
            "devices" : [

            ]
        }, 
        {
            "_id" : ObjectId("5f0c98876f0321f6986755dd"), 
            "name" : "Ruang Tamu", 
            "created_at" : ISODate("2020-07-14T00:23:19.693+0000"), 
            "updated_at" : ISODate("2020-07-14T00:23:19.693+0000"), 
            "devices" : [

            ]
        }
    ]
}

我只想选择 Rooms 字段,按 ObjectID 对内容进行降序排序,并通过此查询将其限制为 1:

I want to select only the Rooms field, sort the content in descending way by ObjectID, and limit it by 1 through this query:

db.users.find({'_id': ObjectId("5f0c64e4dd0a36b93c7deafa")}, {'rooms': 1}).sort({'rooms._id': -1}).limit(1)

但结果不会对字段进行排序,也不会将结果限制为 1.我应该使用什么查询来获得所需的输出?

But the result won't sort the fields and not limiting the result by 1. What query should i use to have the desire output?

推荐答案

您将无法使用顶级 .find 对嵌套数组进行排序或限制,您可以尝试 .aggregate 代替:

You won't be able to do a sort or limit on the nested array with a top-level .find, you can try .aggregate instead:

db.users.aggregate([
  {
    $match: { "_id": ObjectId("5f0c64e4dd0a36b93c7deafa") }
  },
  {
    $project: { rooms: 1 }
  },
  { $unwind: "$rooms" },
  {
    $sort: { "rooms._id": -1 }
  },
  {
    $group: {
      _id: "$_id",
      rooms: { $first: "$rooms" }
    }
  }
])

这篇关于排序和限制嵌入文档 MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆