Mongodb 如何使用id在mongo中的三个集合之间查找结果

Mongodb 如何使用id在mongo中的三个集合之间查找结果,mongodb,Mongodb,我有三套 用户集合: { "_id" : ObjectId("58a0506767535d5cehjk678"), "name" : "someUser", "username" : "someUserName" } 培训: { "_id" : ObjectId("5hkhgec28863b500045ff0d6"), "location" : "somePlace", "coacherId" : "58b9edca8863b500045ff0d0", // user

我有三套

用户集合:

{
  "_id" : ObjectId("58a0506767535d5cehjk678"),
  "name" : "someUser",
  "username" : "someUserName"
}
培训:

{ 
  "_id" : ObjectId("5hkhgec28863b500045ff0d6"),
  "location" : "somePlace",
  "coacherId" : "58b9edca8863b500045ff0d0", // userId
  "maxParticipants" : NumberInt(10),
  "time" : ISODate("2017-03-05T17:00:00.000+0000")
}
用户订单:

{
  "_id" : ObjectId("591iuyt602d3ec00049b7c93"),
  "userId" : "5hkhgec28863b500045ff0d6",
  "trainingId" : "5hkhgec28863b500045ff0d6"
}
如何在两个日期之间找到特定用户的所有培训?
在mongo中,只有3个查询可以实现这一点?

使用MongoDB的聚合功能和聚合阶段可以实现这一点

大概是这样的:

db.userOrders.aggregate(
    [
        {
            $match: {
                "userId" : ObjectId("597c95e3acc4ed0de91a2992") // the id of the user
            }
        },
        {
            $lookup: {
                "from" : "training",
                "localField" : "trainingId",
                "foreignField" : "_id",
                "as" : "training"
            }
        },
        {
            $unwind: {
                path : "$training"
            }
        },
        {
            $match: {
                "training.time" : { $gte: ISODate("2017-03-05T00:00:00.0Z"), $lt: ISODate("2017-03-08T00:00:00.0Z") }
            }
        },
        // if you use MongoDB version >= 3.4
        {
            $replaceRoot: {
                newRoot: "$training"
        }
    }
    ]
);
输出文档示例:

{ 
    "_id" : ObjectId("597c9608acc4ed0de91a2995"), 
    "location" : "somePlace", 
    "coacherId" : "\"Joe\"", 
    "maxParticipants" : NumberInt(10), 
    "time" : ISODate("2017-03-05T17:00:00.000+0000")
}

使用MongoDB的聚合功能和聚合阶段可以实现这一点

大概是这样的:

db.userOrders.aggregate(
    [
        {
            $match: {
                "userId" : ObjectId("597c95e3acc4ed0de91a2992") // the id of the user
            }
        },
        {
            $lookup: {
                "from" : "training",
                "localField" : "trainingId",
                "foreignField" : "_id",
                "as" : "training"
            }
        },
        {
            $unwind: {
                path : "$training"
            }
        },
        {
            $match: {
                "training.time" : { $gte: ISODate("2017-03-05T00:00:00.0Z"), $lt: ISODate("2017-03-08T00:00:00.0Z") }
            }
        },
        // if you use MongoDB version >= 3.4
        {
            $replaceRoot: {
                newRoot: "$training"
        }
    }
    ]
);
输出文档示例:

{ 
    "_id" : ObjectId("597c9608acc4ed0de91a2995"), 
    "location" : "somePlace", 
    "coacherId" : "\"Joe\"", 
    "maxParticipants" : NumberInt(10), 
    "time" : ISODate("2017-03-05T17:00:00.000+0000")
}

为什么需要第三个集合,可以在用户集合中添加培训id,也可以在培训集合中添加用户信息数组。因为我需要有关任何订单的更多详细信息,例如开始日期和日期。为什么需要第三个集合,可以在用户集合中添加培训id,或培训集合中的用户信息数组。因为我需要有关任何订单的更多详细信息,例如开始日期和日期。