Mongodb的联盟

时间:2019-10-22 22:18:18

标签: mongodb mongoose

我一直试图在mongo上解决一个简单的查询

我在两个不同的集合上有两个集合Press Ctrl+E, then press 2 to get split view. Press Ctrl+E, then press 3 to get side-by-side split view. To go to the other split, use Ctrl+E, then press O (alphabet O) Delete current split - Ctrl+E then press 0 (number zero) Delete all splits instead of current - Ctrl+E then press 1 people。在sql上,这些表是我可以做的事情:

orders

但是我如何在mongodb上做呢?我看到了一些带有聚合的解决方案,但是当您加入它们时,这些解决方案具有一些共同点,这些集合完全不同,我只想在联合的顶部进行限制和补偿。

谢谢

1 个答案:

答案 0 :(得分:-1)

尝试此查询

  db.people.aggregate(
  [
    { $limit: 1 },
    { $project: { _id: '$$REMOVE' } },
    { 
         $lookup: { from: 'people', pipeline: [{$project:{Col1:'$firstname'}}], as: 'people' }       
    },
    { 
         $lookup: { from: 'orders', pipeline: [{$project:{Col1:'$name'}}], as: 'orders' } 

    },
    { $project: { union: { $concatArrays: ["$people", "$orders"] } } },
    { $unwind: '$union' },
    { $replaceRoot: { newRoot: '$union' } },
    {$skip: 20},
    {$limit: 10},
  ]);