$ graphLookup管道阶段后,不同的归档结果导致限制

时间:2019-05-05 09:11:14

标签: mongodb aggregation-framework

我想通过$ graphLookup从两个名为peoplebook的集合中得到一个结果,并且likepeople集合中包含{{ 1}}集合book。如何获取_id字段包含like集合的特定计数结果。

现在考虑以下记录:

人员集合

book

藏书

{_id: 1, name: "a", likes: [{_id: 100}, {_id: 101}, ..., {_id: 199}]}
{_id: 2, name: "b", likes: [{_id: 110}, {_id: 111}, ..., {_id: 130}]}
{_id: 3, name: "c", likes: [{_id: 120}, {_id: 121}, ..., {_id: 200}]}
...
{_id: 26, name: "z", likes: [{_id: 130}, {_id: 131}, ..., {_id: 155}]}

和代码:

{_id: 100, name: "book0", price: 15}
{_id: 101, name: "book1", price: 15}
{_id: 102, name: "book2", price: 15}
...
{_id: 200, name: "book200", price: 15}

结果如下:

db.people.aggregate([
  {
    $graphLookup: {
      from: "book",
      startWith: "$likes",
      connectFromField: "likes",
      connectToField: "_id",
      as: "likes"
    }
  },
  // The $limit must be specified here
  {
    $limit: 10 
  }

])

但是我想得到如下结果:

如果“喜欢”字段限制5个结果

[
  {
    {
      _id: 1, 
      name: "a",
      // too much results in likes field
      likes: [
        {_id: 100, name: "book0", price: 15},
        {_id: 101, name: "book1", price: 15},
        {_id: 102, name: "book2", price: 15},
        ...
        {_id: 199, name: "book199", price: 15}
    ]}
  },
  {
    {
      _id: 2, 
      name: "b",
      // too much results in likes field
      likes: [
        {_id: 110, name: "book0", price: 15},
        {_id: 111, name: "book1", price: 15},
        {_id: 112, name: "book2", price: 15},
        ...
        {_id: 130, name: "book199", price: 15}
    ]}
  },
  ...
  {
    {
      _id: 10, 
      name: "j",
      // too much results in likes field
      likes: [
        {_id: 130, name: "book0", price: 15},
        {_id: 131, name: "book1", price: 15},
        {_id: 132, name: "book2", price: 15},
        ...
        {_id: 155, name: "book199", price: 15}
    ]}
  }
]

编辑

如果我按照建议尝试:

[
 // 10 results
  {
    {
      _id: 1, 
      name: "a",
      // 5 results
      likes: [
        {_id: 100, name: "book0", price: 15},
        {_id: 101, name: "book1", price: 15},
        {_id: 102, name: "book2", price: 15},
        {_id: 103, name: "book2", price: 15},
        {_id: 104, name: "book199", price: 15}
    ]}
  },
  {
    {
      _id: 2, 
      name: "b",
      // 5 results
      likes: [
        {_id: 110, name: "book0", price: 15},
        {_id: 111, name: "book1", price: 15},
        {_id: 112, name: "book2", price: 15},
        {_id: 113, name: "book2", price: 15},
        {_id: 114, name: "book199", price: 15}
    ]}
  },
  ...
  {
    {
      _id: 10, 
      name: "j",
      // 5 results
      likes: [
        {_id: 130, name: "book0", price: 15},
        {_id: 131, name: "book1", price: 15},
        {_id: 132, name: "book2", price: 15},
        {_id: 133, name: "book2", price: 15},
        {_id: 134, name: "book199", price: 15}
    ]}
  }
]

然后我收到一个错误:

  

$ in需要一个数组作为第二个参数,发现:丢失

该如何解决?

问题解决了,代码如下

代码

db.people.aggregate([
  { "$lookup": {
    "from": "books",
    "let": { "likes": "$likes" },
    "pipeline": [
      { "$match": {
        "$expr": { "$in": [ "$_id", "$$likes._id" ] }
      }},
     { "$limit": 5 }
   ],
   "as": "likes"
 }}
])

0 个答案:

没有答案