【问题标题】:Concat two collections and skip+limit连接两个集合并跳过+限制
【发布时间】:2020-10-21 03:54:22
【问题描述】:

我有两个集合,集合 A 和集合 B,它们不相关(即没有“外键”的东西)。 它们代表两个相似的数据集,为了论证,假设 collectionA 是鸟类的集合,而 collectionB 是鱼类的集合

我需要同时查询这两个,但使用跳过和限制运算符,因此我需要将查询连接成一个并在执行查询之前应用跳过和限制,因为这些集合有大量的数据会被频繁访问,所以跳过/限制是必要的,但我不希望跳过/限制首先到达collectionA,然后到达collectionB,我希望他们以与_id中的时间戳相同的递增顺序访问文档,所以一个示例结果可能是

start = 3
limit = 2

bird.1 # skip
bird.2 # skip
fish.1 # skip
fish.2 # return this
fish.3 # return this
fish.4 # EOQ
bird.3

我对 MongoDB 还很陌生,所以如果不清楚,我深表歉意。

这是根据请求提供的示例 JSON。 集合非常相似,只有 1 个字段不同(我没有设计这个)

# collectionA
{
  _id: ObjectId('507f1f77bcf86cd799439011'),
  species: 'Falcon',
  beak_type: 'hooked',
  hates_fish: True
}

# collectionB
{
  _id: ObjectId('507f1f77bcf86cd799439012'),
  species: 'Haddock'
  hates_fish: True
}

【问题讨论】:

  • 能否以 JSON 格式添加 A 和 B 的样本收集文档?

标签: python mongodb optimization pymongo


【解决方案1】:

MongoDB 操作符旨在处理单个集合。如果您来自关系数据库背景,这可能看起来很奇怪,因为在 SQL 世界中,您习惯于进行连接以将表链接在一起;但 MongoDB 不是 SQL!

所以看看你的数据,问题真的是:为什么你有两个集合中的数据?数据看起来几乎相同的结构。我会建议有一个单一的集合,然后所有的操作变得更简单。

如果您坚持拥有两个集合,那么本网站上已经有很多关于如何使用聚合查询使用 $lookup 阶段的答案,例如How to join multiple collections with $lookup in mongodb 和其他人。

【讨论】:

    【解决方案2】:
    //you can now use a new feature in Mongo 4.4.1 to combine documents in two collections
    //Working code
    //query data in collection as given in problem statement
    > db.colA.find();
    { "_id" : ObjectId("507f1f77bcf86cd799439011"), "species" : "Falcon", "beak_type" : "hooked", "hates_fish" : "True" }
    > db.colB.find();
    { "_id" : ObjectId("507f1f77bcf86cd799439012"), "species" : "Haddock", "hates_fish" : "True" }
    //use aggregate and $unionWith to acheive your goal
    > db.colA.aggregate([
    ... {$project:{species:1,hates_fish:1,_id:0}},
    ... {$unionWith:{coll:"colB",pipeline:[{$project:{species:1,hates_fish:1,_id:0}}]}}
    ... ]);
    { "species" : "Falcon", "hates_fish" : "True" }
    { "species" : "Haddock", "hates_fish" : "True" }
    > print("MongoDB",db.version());
    MongoDB 4.4.1
    >
    //if you currently don't have 4.4.1 and you want to use this feature for your task, 
    //please upgrade to Mongo DB 4.4.1, sooner or later you need to upgrade!
    //you can further add sort and skip commands in the same aggregate query
    //example below:
    > db.colA.aggregate([
    ... {$project:{species:1,hates_fish:1,_id:0}},
    ... {$unionWith:{coll:"colB",pipeline:[{$project:{species:1,hates_fish:1,_id:0}}]}},
    ... {$sort:{species:-1}}
    ... ]);
    { "species" : "Haddock", "hates_fish" : "True" }
    { "species" : "Falcon", "hates_fish" : "True" }
    > db.colA.aggregate([
    ... {$project:{species:1,hates_fish:1,_id:0}},
    ... {$unionWith:{coll:"colB",pipeline:[{$project:{species:1,hates_fish:1,_id:0}}]}},
    ... {$sort:{species:-1}},
    ... {$skip:1}
    ... ]);
    { "species" : "Falcon", "hates_fish" : "True" }
    >
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      • 1970-01-01
      • 2021-05-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多