Concat两个集合并跳过+限制

2024-04-29 20:13:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个集合,collectionA和collectionB,它们不相关(即没有“外键”的东西)。 它们代表了两个相似的数据集,比如说collectionA是鸟类物种的集合,collectionB是鱼类物种的集合

我需要同时查询这两个,但使用skip和limit运算符,因此我需要将查询合并为一个查询,并在执行查询之前应用skip和limit,因为集合中有大量的数据将被频繁访问,因此skip/limit是必要的,但是我不希望跳过/限制简单地首先到达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示例。 这些集合非常相似,只有一个字段不同(我没有设计这个)

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

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

Tags: 数据idtrue示例return物种thisspecies
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" }
>

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

所以看看你的数据,问题是:为什么你有两个收集的数据?数据看起来几乎是相同的结构。我建议使用单个集合,这样所有操作都会变得更简单

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

相关问题 更多 >