简体   繁体   English

嵌套模式/子文档的 object 中的 ZCCADCDEDB567ABAE643E15DCF0974E503Z findById() - 聚合

[英]Mongoose findById() in an object of nested schemas / subdocuments - aggregation

I have an object types .我有一个 object types It contains multiple schemas.它包含多个模式。 I need to find an item by its id, but the item could be in exampleOne or exampleTwo (Note: More then two schemas will be used).我需要通过它的 id 找到一个项目,但该项目可能在exampleOneexampleTwo中(注意:将使用两个以上的模式)。

For example, querying for "id: 608a5b290e635ece6828141e" on this:例如,在此查询"id: 608a5b290e635ece6828141e"

{
  "_id": "608642db80a36336946620aa",
  "title": "titleHere",
  "types": {
    "exampleOne": [
      {
        "_id": "6086430080a36336946620ab",
        "front": "front",
        "back": "back"
      },
      {
        "_id": "608a5b186ee1598ac9c222b4",
        "front": "front2",
        "back": "back2"
      }
    ],
    "exampleTwo": [
      {
        "_id": "608a5b290e635ece6828141e", // the queried document
        "normal": {
          "front": "2front",
          "back": "2back"
        },
        "reversed": {
          "front": "2frontReversed",
          "back": "2backReversed"
        }
      },
      {
        "_id": "608a5b31a3f9806de2537269",
        "normal": {
          "front": "2front2",
          "back": "2back2"
        },
        "reversed": {
          "front": "2frontReversed2",
          "back": "2backReversed2"
        }
      }
    ]
  }
}

should return:应该返回:

{
  "_id": "608a5b290e635ece6828141e",
  "normal": {
    "front": "2front",
    "back": "2back"
  },
  "reversed": {
    "front": "2frontReversed",
    "back": "2backReversed"
  }
},

Ideally, the solution would only require one search.理想情况下,该解决方案只需要一次搜索。 I did some research on this but couldn't figure out how to search all objects inside types without creating a search for each schema and seeing if any of them returned a result.我对此进行了一些研究,但无法弄清楚如何在不为每个模式创建搜索并查看它们中的任何一个是否返回结果的情况下搜索types内的所有对象。

Here are my schemas, if they are needed:如果需要,这是我的模式:

var MainSchema = new Schema ({
  title: { type: String, required: true, maxlength: 255 },
  types: {
    exampleOne: [exampleOneSchema],
    exampleTwo: [exampleTwoSchema],
  }
});
var exampleOneSchema = new Schema({
    front: {type: String, required: true},
    back: {type: String, required: true},
});
var exampleTwoSchema= new Schema({
    normal: {
      front: {type: String, required: true},
      back: {type: String, required: true},
    },
    reversed: {
      front: {type: String, required: true},
      back: {type: String, required: true},
    },
});

All help is appreciated!感谢所有帮助!

Thanks,谢谢,

Sour_Tooth

Demo - https://mongoplayground.net/p/t5VYdkrL_nC演示 - https://mongoplayground.net/p/t5VYdkrL_nC

db.collection.aggregate([
  {
    $match: { // filter the document so uniwnd and group have only 1 record to deal with
      $or: [
        { "types.exampleOne._id": "608a5b290e635ece6828141e" },
        { "types.exampleTwo._id": "608a5b290e635ece6828141e" }
      ]
    }
  },
  {
    $group: {
      _id: "$_id",
      docs: { $first: { "$concatArrays": [ "$types.exampleOne", "$types.exampleTwo" ] } } // join both array into 1 element
    }
  },
  { $unwind: "$docs" }, //  break into individual documents
  {
    $match: { // filter the records
     "docs._id": "608a5b290e635ece6828141e"
    }
  },
  { $replaceRoot: { "newRoot": "$docs" } } // set it to root
])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM