从两个集合mongodb中选择数据

时间:2017-05-17 20:20:53

标签: javascript angularjs mongodb

我们有两台服务器,有些数据丢失,所以我需要检查究竟是什么缺失并修复它。订单的数据如下所示:

 {
        "_id" : ObjectId("591a067bd7bc153c9bd4b5c2"),
        "customer" : "cus_AeMs19HKhEZDY7",
        "paymentID" : "ch_1AFb9pEz37oqk2rHVkbAfNrU",
        "paymentStatus" : "PAID",
        "user" : "melaniecarr23@gmail.com",
        "createdOn" : ISODate("2017-05-15T19:50:19.775Z"),
        "cart" : {
                "username" : "melaniecarr23@gmail.com",
                "totalQty" : 1,
                "totalPrice" : 50,
                "items" : [
                        {
                                "players" : [
                                        {
                                                "country" : "jlk",
                                                "zip" : "jkl",
                                                "state" : "jkl",
                                                "city" : "jkl",
                                                "address" : "jkl",
                                                "phone" : "432",
                                                "email" : "drmelaniecarr@gmail.com",
                                                "last" : "Snape",
                                                "first" : "Severus"
                                        },
                                        {
                                                "adult" : null,
                                                "country" : "jlk",
                                                "zip" : "jlk",
                                                "state" : "jkl",
                                                "city" : "jkl",
                                                "address" : "jkl",
                                                "phone" : "432",
                                                "email" : "melaniecarr23@gmail.com",
                                                "last" : "Potter",
                                                "first" : "Harry"
                                        }
                                ],
                                "price" : 50,
                                "numplayers" : 2,
                                "group" : "nonpro",
                                "level" : "12s",
                                "division" : "Junior Boys",
                                "field" : "Main",
                                "day" : "Friday",
                                "event" : "Junior Boys 12s",
                                "id" : "58d89697ffc0346230a43a8d"
                        }
                ]
        },
        "__v" : 0
}

对于每个cart.item,注册集合中都有一个文档,如下所示:

{
    "_id" : ObjectId("590aa7a13c85042478800c52"),
    "event" : "Women's B",
    "paymentId" : "ch_1AFb9pEz37oqk2rHVkbAfNrU",
    "createdOn" : ISODate("2017-05-13T06:24:07.609Z"),
    "paymentID" : "VOLUNTEER",
    "paymentStatus" : "PAID",
    "__v" : 1

}

我需要计算orders.cart.items数组中的项目数量,看看它们是否与注册结果的数量相匹配,其中paymentId与订单中的结果相同。 如果它们不相同,我需要看到这些结果。

有人可以帮我理解如何用mongo做到这一点吗?

1 个答案:

答案 0 :(得分:0)

实际上,我想出了这个!我不得不在聚合管道中使用$ lookup:

Order
  .aggregate([     
    {       $lookup:         
    {           from: "registrations",
                localField: "paymentID",           
                foreignField: "paymentId",           
                as: "REGISTRATIONS"         
    }    
    } 
  ]);

这允许我使用ng-repeat和ng-if来显示cart.items和REGISTRATIONS数组的长度并进行比较。如果它们不同,我会显示每个内容。这正是我所寻找的,而且非常快!得爱聚集。