comapare id in数组并通过查找mongoDB获取值

时间:2018-05-04 12:51:31

标签: node.js mongodb express mongoose

我的收藏结构如下:

 {"_id" : ObjectId("5aec2ce5020ba15d2fb2665a"),
"password" : "e10adc3949ba59abbe56e057f20f883e",
"unique_id" : 22,
"first_name" : "Foram",
"last_name" : "Test ",
"country_phone_code" : "+61",
"email" : "a@a.com",
"phone" : "1231231231",
"device_type" : "android",
"admintypeid" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"vehicle_detail" : [ 
    {
        "service_name" : "You",
        "_id" : ObjectId("5aec2d08020ba15d2fb2665c"),
        "name" : "Qee",
        "plate_no" : "Qwe111",
        "model" : "Qee",
        "color" : "Blue",
        "passing_year" : "2005",
        "service_type" : [ 
            ObjectId("5a9f9c53b9e8fa66f9b934c1")
        ],
        "admin_type_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
        "is_selected" : true,
        "pictureData" : "vehicle_images/5aec2d08020ba15d2fb2665cWjyP.jpg"
    }, 
    {
        "pictureData" : "vehicle_images/5aec35e00efc106080e14ec7y2tO.jpg",
        "is_selected" : false,
        "admin_type_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
        "service_type" : [ 
            ObjectId("5a9f9c53b9e8fa66f9b934c1"), 
            ObjectId("5ac954b7075e16583dc3311f")
        ],
        "passing_year" : "2018",
        "color" : "No",
        "model" : "Bcs",
        "plate_no" : "12112",
        "name" : "Bcs",
        "_id" : ObjectId("5aec35e00efc106080e14ec7"),
        "service_name" : "You"
    }
],
"service_type" : [ 
    ObjectId("5a9f9c53b9e8fa66f9b934c1")
],
"is_vehicle_document_uploaded" : true,
"is_trip" : [],
"__v" : 14
}

我想在我的service_type数组中使用camp_detail数组中的camp 到另一个名为city_types的集合,而不是我必须将city_types集合的类型映射到tyes集合并从类型集合中获取名称的值

我的city_type集合将如下所示

{
"_id" : ObjectId("5a9f9c53b9e8fa66f9b934c1"),
"countryid" : ObjectId("5abb275fd20731597cc01229"),
"cityid" : ObjectId("5abb27cbd20731597cc0122a"),
"typeid" : ObjectId("5a9f9b55b9e8fa66f9b934c0")
"__v" : 0}

包含type_id

我的类型集合如下所示

{
"_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
"typename" : "You",
"description" : "KARRU You",
"__v" : 0,
"main_type" : 0

}

我想要它的类型名称

来自id

我使用过以下代码:

exports.get_provider_vehicle_list = function (req, res, next) {

var mongoose = require('mongoose');
var Schema = mongoose.Types.ObjectId;
var condition = {$match: {"_id": Schema(req.body.provider_id)}};
var vunwind = {$unwind: "$vehicle_detail"}

var lookup = {
    $lookup:
            {
                from: "types",
                localField: "vehicle_detail.admin_type_id",
                foreignField: "_id",
                as: "type_detail"
            }
};
var unwind = {$unwind: {
        path: "$type_detail",
        preserveNullAndEmptyArrays: true
    }
};

var group = {$group: {
        _id: null,
        "vehicle_detail": {$push: {
                is_selected: "$vehicle_detail.is_selected",
                admin_type_id: "$vehicle_detail.admin_type_id",
                service_type: "$vehicle_detail.service_type",
                passing_year: "$vehicle_detail.passing_year",
                color: "$vehicle_detail.color",
                model: "$vehicle_detail.model",
                plate_no: "$vehicle_detail.plate_no",
                name: "$vehicle_detail.name",
                _id: "$vehicle_detail._id",
                type_image_url: '$type_detail.type_image_url',
                service_name:"$vehicle_detail.service_name",
                pictureData:"$vehicle_detail.pictureData"
            }}
    }
}
Provider.aggregate([condition, vunwind, lookup, unwind, group], function (err, provider) {

    if (err || provider.length == 0) {
        res.json({success: true, vehicle_list: []})
    } else {
        res.json({success: true, vehicle_list: provider[0].vehicle_detail})
    }

})

};

我的资源如下:

{
"success": true,
"vehicle_list": [
    {
        "is_selected": true,
        "admin_type_id": "5a9f9b55b9e8fa66f9b934c0",
        "service_type": [
            "5a9f9c53b9e8fa66f9b934c1",
            "5ac954b7075e16583dc3311f"
        ],
        "passing_year": "2005",
        "color": "Blue",
        "model": "Qee",
        "plate_no": "Qwe111",
        "name": "Qee",
        "_id": "5aec2d08020ba15d2fb2665c",
        "type_image_url": "service_type_images/5a9f9b55b9e8fa66f9b934c0mjYA.png",
        "service_name": "You",
        "pictureData": "vehicle_images/5aec2d08020ba15d2fb2665cWjyP.jpg"
    }
]

}

和res我想要的是

{
"success": true,
"vehicle_list": [
    {
        "is_selected": true,
        "admin_type_id": "5a9f9b55b9e8fa66f9b934c0",
        "service_type": [
            "5a9f9c53b9e8fa66f9b934c1",
            "5ac954b7075e16583dc3311f"
        ],
        "passing_year": "2005",
        "color": "Blue",
        "model": "Qee",
        "plate_no": "Qwe111",
        "name": "Qee",
        "_id": "5aec2d08020ba15d2fb2665c",
        "type_image_url": "service_type_images/5a9f9b55b9e8fa66f9b934c0mjYA.png",
        "service_type_name": [
            "You"
        ],
        "service_name": "You",
        "pictureData": "vehicle_images/5aec2d08020ba15d2fb2665cWjyP.jpg"
    }
]

}

2 个答案:

答案 0 :(得分:2)

请检查。

db.getCollection('vehicle').aggregate([
  {
      $unwind: {
        path: '$vehicle_detail',
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $unwind: {
        path: '$vehicle_detail.service_type',
        preserveNullAndEmptyArrays: true,
      },
    },
     {
      $lookup: {
        from: 'city_type',
        localField: 'vehicle_detail.service_type',
        foreignField: '_id',
        as: 'city_type',
      },
    },
    {
      $unwind: {
        path: '$city_type',
        preserveNullAndEmptyArrays: true,
      },
    },
      {
      $lookup: {
        from: 'types',
        localField: 'city_type.typeid',
        foreignField: '_id',
        as: 'types',
      },
    },
    {
      $unwind: {
        path: '$types',
        preserveNullAndEmptyArrays: false,
      },
    },
    {
        $group: {
          _id: '$_id',
          first_name: { $first: 1 }, 
          last_name: { $first: 1 }, 
          types: { $first: '$types' },
        },
      },
])

结果

{
    "_id" : ObjectId("5aec2ce5020ba15d2fb2665a"),
    "first_name" : 1.0,
    "last_name" : 1.0,
    "types" : {
        "_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
        "typename" : "You",
        "description" : "KARRU You",
        "__v" : 0,
        "main_type" : 0
    }
}

注意:您可以在群组中添加所需的字段。

答案 1 :(得分:0)

我从你的解释中发送我理解的内容。我希望它有所帮助。

有些观点:

  1. 我将你的对象city_types和类型更改为数组,因为你说了Collections;
  2. 我将ID链接到Vehicle-> city-> types

            function ObjectId(id){
                return id;
            }
    
            var obj = {"_id" : ObjectId("5aec2ce5020ba15d2fb2665a"),
                "password" : "e10adc3949ba59abbe56e057f20f883e",
                "unique_id" : 22,
                "first_name" : "Foram",
                "last_name" : "Test ",
                "country_phone_code" : "+61",
                "email" : "a@a.com",
                "phone" : "1231231231",
                "device_type" : "android",
                "admintypeid" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
                "vehicle_detail" : [ 
                {
                    "service_name" : "Service 1",
                    "_id" : ObjectId("5aec2d08020ba15d2fb2665c"),
                    "name" : "Qee",
                    "plate_no" : "Qwe111",
                    "model" : "Qee",
                    "color" : "Blue",
                    "passing_year" : "2005",
                    "service_type" : [ 
                        ObjectId("5a9f9c53b9e8fa66f9b934c1")
                    ],
                    "admin_type_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
                    "is_selected" : true,
                    "pictureData" : "vehicle_images/5aec2d08020ba15d2fb2665cWjyP.jpg"
                }, 
                {
                    "pictureData" : "vehicle_images/5aec35e00efc106080e14ec7y2tO.jpg",
                    "is_selected" : false,
                    "admin_type_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
                    "service_type" : [ 
                        ObjectId("5a9f9c53b9e8fa66f9b934c1"), 
                        ObjectId("5ac954b7075e16583dc3311f")
                    ],
                    "passing_year" : "2018",
                    "color" : "No",
                    "model" : "Bcs",
                    "plate_no" : "12112",
                    "name" : "Bcs",
                    "_id" : ObjectId("5aec35e00efc106080e14ec7"),
                    "service_name" : "Service 2"
                }
            ],
            "service_type" : [ 
                ObjectId("5a9f9c53b9e8fa66f9b934c1")
            ],
            "is_vehicle_document_uploaded" : true,
            "is_trip" : [],
            "__v" : 14
            };
    
            var city_types = [{
                "_id" : ObjectId("5aec2d08020ba15d2fb2665c"),
                "countryid" : ObjectId("5abb275fd20731597cc01229"),
                "cityid" : ObjectId("5abb27cbd20731597cc0122a"),
                "typeid" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
                "__v" : 0
            }];
    
            var types = [{
                "_id" : ObjectId("5a9f9b55b9e8fa66f9b934c0"),
                "typename" : "You",
                "description" : "KARRU You",
                "__v" : 0,
                "main_type" : 0
            }];
    
            function print() {
                    //iterate over vehicle_detail
                    obj.vehicle_detail.forEach((vehicle)=>{
                        console.log("vehicle="+ vehicle._id);
                        //iterate over cities type
                        city_types.forEach((city)=> {
                            console.log("city="+ city._id);
                            if (city._id == vehicle._id) {
                                //iterate over types
                                types.forEach((type)=> {
                                    console.log("type="+ type._id);
                                    if (city.typeid == type._id) {
                                       console.log("Type Name: "+type.typename);
                                    }
                                })
                            }
                        })
                    });
    
            }
    
            print();
    
  3. 该计划的输出是:

    >node index4.js
    vehicle=5aec2d08020ba15d2fb2665c
    city=5aec2d08020ba15d2fb2665c
    type=5a9f9b55b9e8fa66f9b934c0
    Type Name: You
    vehicle=5aec35e00efc106080e14ec7
    city=5aec2d08020ba15d2fb2665c