在嵌套文档中查找数组并对其进行排序

时间:2020-07-15 05:38:20

标签: mongodb

我的收集模式如下:

{ 
    "_id" : ObjectId("5f0c64e4dd0a36b93c7deafa"), 
    "name" : "Asd", 
    "email" : "asd@asd.com", 
    "password" : "$2b$12$66OTK8mSWELMF5YiF9HMUuHEeOVLI61aINjWs1Cmn1699lLJfz/7y", 
    "auto_ml" : true, 
    "notification" : true, 
    "photo" : null, 
    "tariff_id" : NumberInt(1), 
    "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" : [
        
    ], 
    "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("5f0c98876f0321f6986755dd"), 
            "name" : "Ruang Tamu", 
            "created_at" : ISODate("2020-07-14T00:23:19.693+0000"), 
            "updated_at" : ISODate("2020-07-14T19:00:08.281+0000"), 
            "devices" : [
                {
                    "serial_number" : "ST9L0CY4A2AVY7HFWWUE", 
                    "used_relay" : NumberInt(0), 
                    "created_at" : ISODate("2020-07-14T16:56:22.156+0000"), 
                    "updated_at" : ISODate("2020-07-14T16:56:22.156+0000"), 
                    "sensors" : [
                        {
                            "_id" : ObjectId("5f0db478ca203bde99d2438e"), 
                            "name" : "Temperature", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T20:34:48.134+0000"), 
                            "updated_at" : ISODate("2020-07-14T20:34:48.134+0000")
                        }, 
                        {
                            "_id" : ObjectId("5f0dbe0563ccbcb2a2aecc04"), 
                            "name" : "Motion", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T21:15:33.135+0000"), 
                            "updated_at" : ISODate("2020-07-14T21:15:33.135+0000")
                        }, 
                        {
                            "_id" : ObjectId("5f0dc0412022e93af338316f"), 
                            "name" : "Humidity", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T21:25:05.126+0000"), 
                            "updated_at" : ISODate("2020-07-14T21:25:05.126+0000")
                        }, 
                        {
                            "_id" : ObjectId("5f0dc0442022e93af3383170"), 
                            "name" : "Light", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T21:25:08.451+0000"), 
                            "updated_at" : ISODate("2020-07-14T21:25:08.451+0000")
                        }
                    ], 
                    "switches" : [

                    ]
                }
            ]
        }
    ]
}

问题是,我如何才能访问带有"serial_number": "ST9L0CY4A2AVY7HFWWUE"的设备内部和ID为ObjectId("5f0c98876f0321f6986755dd")的房间内部的阵列传感器,并按ID对传感器进行排序?我尝试了投影方式和聚合方式,但结果与我预期的不同。我的预期结果只是显示了像这样的传感器阵列:

"sensors" : [
                        {
                            "_id" : ObjectId("5f0db478ca203bde99d2438e"), 
                            "name" : "Temperature", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T20:34:48.134+0000"), 
                            "updated_at" : ISODate("2020-07-14T20:34:48.134+0000")
                        }, 
                        {
                            "_id" : ObjectId("5f0dbe0563ccbcb2a2aecc04"), 
                            "name" : "Motion", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T21:15:33.135+0000"), 
                            "updated_at" : ISODate("2020-07-14T21:15:33.135+0000")
                        }, 
                        {
                            "_id" : ObjectId("5f0dc0412022e93af338316f"), 
                            "name" : "Humidity", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T21:25:05.126+0000"), 
                            "updated_at" : ISODate("2020-07-14T21:25:05.126+0000")
                        }, 
                        {
                            "_id" : ObjectId("5f0dc0442022e93af3383170"), 
                            "name" : "Light", 
                            "value" : null, 
                            "created_at" : ISODate("2020-07-14T21:25:08.451+0000"), 
                            "updated_at" : ISODate("2020-07-14T21:25:08.451+0000")
                        }
                    ], 

我尝试过的查询:

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

])

但是sensors似乎没有排序。 预先感谢您的帮助〜

0 个答案:

没有答案