如何对“作者"进行查找. mongodb中的各种数组的字段? [英] How to make a lookup of the "Author" field of various arrays in mongodb?

查看:119
本文介绍了如何对“作者"进行查找. mongodb中的各种数组的字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是我想对对象评论",关注者"和观看"的数组进行作者"字段的查找,但是我不知道为什么它会在其他对象中给我这个结果数组,该值将在审阅"数组中重复相同次数的文档.

My problem is that I want to do a Lookup of the field "Author" for the array of objects "Reviews", "Followers" and "Watching" but I don't know why it gives me this result in the others arrays, that value repeats the same number of times of the documents in the "Reviews" array.

  .unwind({ path: '$reviews', preserveNullAndEmptyArrays: true })
  .lookup({
    from: 'users',
    let: { userId: '$reviews.author' },
    pipeline: [
      { $match: { $expr: { $eq: ['$_id', '$$userId'] } } },
      {
        $project: {
          name: 1,
          username: 1,
          photo: 1,
          rank: 1,
          'premium.status': 1,
          online: 1,
        },
      },
    ],
    as: 'reviews.author',
  })
  .unwind({ path: '$followers', preserveNullAndEmptyArrays: true })
  .lookup({
    from: 'users',
    let: { userId: '$followers.author' },
    pipeline: [
      { $match: { $expr: { $eq: ['$_id', '$$userId'] } } },
      {
        $project: {
          name: 1,
          username: 1,
          photo: 1,
          rank: 1,
          'premium.status': 1,
          online: 1,
        },
      },
    ],
    as: 'followers.author',
  })
  .unwind({ path: '$watching', preserveNullAndEmptyArrays: true })
  .lookup({
    from: 'users',
    let: { userId: '$watching.author' },
    pipeline: [
      { $match: { $expr: { $eq: ['$_id', '$$userId'] } } },
      {
        $project: {
          name: 1,
          username: 1,
          photo: 1,
          rank: 1,
          'premium.status': 1,
          online: 1,
        },
      },
    ],
    as: 'watching.author',
  })
  .group({
    _id: '$_id',
    data: {
      $first: '$$ROOT',
    },
    reviews: {
      $push: '$reviews',
    },
    followers: {
      $push: '$followers',
    },
    watching: {
      $push: '$watching',
    },
  })

这是评论"包含文档时的结果:

This is the result when "Reviews" has documents:

跟随者/监视"数组在数据库中没有任何内容,但是以这种方式在此处显示,重复该值与正在审阅的文档数量相同,我不知道会发生什么.

The "Followers / Watching" array has nothing in the database but it is shown here in this way, repeating that value the same number of documents that are in reviews, I don't know what happens.

当所有数组都为空时,会发生这种情况:

And when all arrays are empty, this happens:

它不断显示,但我不知道如何修复.

It keeps showing that, but I don't know how to repair it.

总而言之,评论",观看"和关注者"都有一个作者"字段,我想对观看的作者字段以及关注者和评论进行查找,但是我遇到了这些问题.请我帮忙.

In summary, "Reviews", "Watching", and "Followers" have an "Author" field, and I want to do a lookup to the author field of watching, and also for followers and reviews but I have these problems. Please I need help.

示例:这是它在数据库中的外观:

Example: This is how it looks in the database:

非常感谢您.

推荐答案

$unwind阶段为要展开的数组中的每个元素创建一个新文档.每个新文档将包含该文档中所有其他字段的副本.

The $unwind stage creates a new document for every element in the array you are unwinding. Each new document will contain a copy of every other field in the document.

如果原始文档看起来像

{
  _id: "unique",
  Array1:["A","B","C"],
  Array2:["D","E","F"],
}

展开"Array1"后,管道中将包含3个文档:

After unwinding "Array1", there will be 3 documents in the pipeline:

[
 {
   _id: "unique",
   Array1:"A",
   Array2:["D","E","F"]
 },{
   _id: "unique",
   Array1:"B",
   Array2:["D","E","F"]
 },{
   _id: "unique",
   Array1:"C",
   Array2:["D","E","F"]
 }
]

然后展开的观察程序"将展开观察程序数组的每个,以便您具有数组的笛卡尔积. 游乐场

Then unwinding "watchers" will expand each of the watchers arrays so that you have the cartesian product of the arrays. Playground

在您的情况下,原始文档有2条评论,但没有关注者,也没有观察者,因此在管道开始时包含一个文档,类似于:

In your case, the original document has 2 reviews, but no followers and no watchers, so at the start of the pipeline contains one document, similar to:

[
  {
    _id: "ObjectId",
    data: "other data"
    reviews: [{content:"review1", author:"ObjectId"},
              {content:"review2", author:"ObjectId"}]
  }
]

第一次展开后,您有2个文档:

After the first unwind, you have 2 documents:

[
  {
    _id: "ObjectId",
    data: "other data"
    reviews: {content:"review1", author:"ObjectId"}
  },
{
    _id: "ObjectId",
    data: "other data"
    reviews: {content:"review2", author:"ObjectId"}
  }
]

第一次查找将作者ID替换为每个文档的数据,然后将第二个展开应用于每个文档.

The first lookup replaces the author ID with data for each document, then the second unwind is applied to each document.

由于该数组为空,查找将返回一个空数组,并应用第三个展开. 在$group阶段之前,管道包含2个带有数组的文档:

Since that array is empty, the lookup returns an empty array, and the third unwind is applied. Just before the $group stage, the pipeline contains 2 documents with the arrays:

[
  {
    _id: "ObjectId",
    data: "other data"
    reviews: {content:"review1", author:"ObjectId"},
    followers: {author: []},
    watchers: {author: []}
  },
{
    _id: "ObjectId",
    data: "other data"
    reviews: {content:"review2", author:"ObjectId"},
    followers: {author:[]},
    watchers: {author: []}
  }
]

由于两个文档具有相同的_id,因此将它们分组在一起,最终结果将第一个文档包含为数据".

Since both documents have the same _id they are grouped together, with the final result containing the first document as "data".

对于数组,遇到每个文档时,将相应字段的值压入数组,从而使每个数组具有2个值.

For the arrays, as each document is encountered, the value of the corresponding field is pushed onto the array, resulting in each array having 2 values.

这篇关于如何对“作者"进行查找. mongodb中的各种数组的字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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