联合2表像一个实体一样,然后进行排序,跳过和限制猫鼬

问题描述

我有两个架构,架构A和B如下:

const A = new Schema({
  paymentId: Number,date: Date,...data
})

const B = new Schema({
  paidId: Number,...data
})

我想从A和B都返回记录,就像它是一个表一样,在这里我可以从A和B都获得可以与.sort().skip()和{{ 1}}具有理想的功能。

我可以在两个表上都做一个.limit(),将它们连接起来,然后手动排序/跳过/限制,但是我发现效率很低。

编辑:进行澄清。两个集合是否相关都无关紧要。我只想从两个集合中查询,就像两个都在一个集合中一样。

例如,如果我有以下文件

.find()

使用选项// Documents in A { date: '2020-01-01',A_id: 1 },{ date: '2020-01-03',A_id: 2 },{ date: '2020-01-05',A_id: 3 },// Documents in B { date: '2020-01-02',B_id: 1 },{ date: '2020-01-04',B_id: 2 },{ date: '2020-01-06',B_id: 3 },进行查询将导致以下结果:

.sort('date').skip(0).limit(5)

解决方法

//Can I suggest,$merge to merge these two independent collections into another different collection 
//and then use aggregation to doing sort(),skip() and limit()
> db.version();
4.2.6
> db.colA.find();
{ "_id" : ObjectId("5f76d969975ec8826bbcaab5"),"date" : "2020-01-01","A_id" : 1 }
{ "_id" : ObjectId("5f76d969975ec8826bbcaab6"),"date" : "2020-01-03","A_id" : 2 }
{ "_id" : ObjectId("5f76d969975ec8826bbcaab7"),"date" : "2020-01-05","A_id" : 3 }
> db.colB.find();
{ "_id" : ObjectId("5f76d969975ec8826bbcaab8"),"date" : "2020-01-02","B_id" : 1 }
{ "_id" : ObjectId("5f76d969975ec8826bbcaab9"),"date" : "2020-01-04","B_id" : 2 }
{ "_id" : ObjectId("5f76d969975ec8826bbcaaba"),"date" : "2020-01-06","B_id" : 3 }
> db.colA.aggregate([
{$match:{}},{$merge:{into: "colAB"}}
]);
>db.colB.aggregate([
{$match:{}},{$merge:{into: "colAB"}}
]);
> db.colAB.find();
{ "_id" : ObjectId("5f76d969975ec8826bbcaab5"),"A_id" : 1,"date" : "2020-01-01" }
{ "_id" : ObjectId("5f76d969975ec8826bbcaab6"),"A_id" : 2,"date" : "2020-01-03" }
{ "_id" : ObjectId("5f76d969975ec8826bbcaab7"),"A_id" : 3,"date" : "2020-01-05" }
{ "_id" : ObjectId("5f76d969975ec8826bbcaab8"),"B_id" : 1,"date" : "2020-01-02" }
{ "_id" : ObjectId("5f76d969975ec8826bbcaab9"),"B_id" : 2,"date" : "2020-01-04" }
{ "_id" : ObjectId("5f76d969975ec8826bbcaaba"),"B_id" : 3,"date" : "2020-01-06" }
> > db.colAB.aggregate([
... {$project:{_id:0}},... {$sort:{date:1}},... {$skip:0},... {$limit:5}
... ]);
{ "A_id" : 1,"date" : "2020-01-01" }
{ "B_id" : 1,"date" : "2020-01-02" }
{ "A_id" : 2,"date" : "2020-01-03" }
{ "B_id" : 2,"date" : "2020-01-04" }
{ "A_id" : 3,"date" : "2020-01-05" }
,

来自https://stackoverflow.com/a/55289023/3793648

可以使用“ SQL UNION”方式在MongoDB中进行联合 在单个查询中聚合和查找。

类似这样的东西:

    db.getCollection("AnyCollectionThatContainsAtLeastOneDocument").aggregate(
    [
      { $limit: 1 },// Reduce the result set to a single document.
      { $project: { _id: 1 } },// Strip all fields except the Id.
      { $project: { _id: 0 } },// Strip the id. The document is now empty.

      // Lookup all collections to union together.
      { $lookup: { from: 'collectionToUnion1',pipeline: [...],as: 'Collection1' } },{ $lookup: { from: 'collectionToUnion2',as: 'Collection2' } },{ $lookup: { from: 'collectionToUnion3',as: 'Collection3' } },// Merge the collections together.
      {
        $project:
        {
          Union: { $concatArrays: ["$Collection1","$Collection2","$Collection3"] }
        }
      },{ $unwind: "$Union" },// Unwind the union collection into a result set.
      { $replaceRoot: { newRoot: "$Union" } } // Replace the root to cleanup the resulting documents.
    ]);

更多细节在上面的帖子中。添加$sort$skip$limit只是将它们添加到聚合管道中的问题。非常感谢@sboisse!

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...