查找嵌入的文档 ID [英] Find Embedded Document ID

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

问题描述

关于我之前的帖子这里,使用相同的集合模式(我应该在这里重写它)

Regarding to my previous post here, with the same collection schema (i should've rewrite it here)

{ 
    "_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" : [

            ]
        }
    ]
}

我想找到与 _idrooms._id 匹配的房间.我正在运行此查询,但结果与我预期的不一样:

I want to find rooms that match with _id and rooms._id. I'm running this query but the result is not as i expected:

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

问题似乎出现在累加器表达式上,而我使用 $first 并且结果显示嵌入文档中的第一个房间.如何找到 $match 中写的确切内容?

The problem seems on the accumulator expression, whereas i use $first and the result is showing the first room in the embedded documents. How to find the exact thing as written in $match?

推荐答案

在调整匹配查询后,我想出了它应该的方式:

After tweaking the match query, i figured out the way it should be:

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

必须有 2 个 $matches,不要与 $and 运算符比较

There must to be 2 $matches, not comparing it with $and operator

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

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