在mongodb中基于对象数组中的字段查找特定对象

时间:2018-11-21 07:05:06

标签: node.js mongodb express mongoose

我正在尝试在express.js mongoDB中创建一个项目。但是,我在mongoDB中从数组ob对象中搜索数据时遇到了一些问题。

vehicle.js(模型)

const mongoose = require('mongoose');
const Joi = require('joi');

const vehicleSchema = mongoose.Schema({
    category: {
        type: String,
        required: [true, "category name is required!"],
        minlength: 3,
        maxlength: 50
    },

    name: {
        type: String,
        required: [true, 'vehicle name is required'],
        minlength: 3,
        maxlength: 100
    },

    brand: {
        type: String,
        required: [true, 'vehicle brand is required'],
        minlength: 3,
        maxlength: 40
    },

    releseDate: {
        type: Date,
        default: Date.now
    },

    otherProperties: [
        {
            name: {
                type: String,
                required: [true, 'property name is required']
            },

            value: {
                type: String,
                required: [true, 'property value is required']
            },

            menu: {
                type: String,
                required: [true, 'property menu is required']
            }
        }
    ],

    price: {
        type: Number,
        required: [true, 'vehicle price is required']
    },
});

exports.Vehicle = mongoose.model('vehicle', vehicleSchema);

vehicleController.js

exports.getOtherPopertyByVehicleId = async (req, res) => {
    const { vehicleId } = req.params;

    if (!mongoose.Types.ObjectId.isValid(vehicleId))
        return res.status(400).send({ ...failed, message: 'invalid vehicle id!' });

    const otherPopertyData = await Vehicle.find({
        _id: vehicleId,
        'otherProperties.menu': 'feuelEfficiency'
    }).select('otherProperties');

    if (otherPopertyData.length === 0) return res.status(404).send({ ...failed, message: `no properties is found!` });
    console.log(otherPopertyData);

    res.send({
        ...success,
        data: otherPopertyData.otherProperties || 100,
        message: `properties retrives successfully.`
    });
};

当我通过vehicleId(5bebf4561708fd40ea4af125)和menu(brakeWheel)从otherProperty中搜索车辆属性时,它将返回该车辆的所有属性对象。但是我想获取包含该菜单(brakeWheel)的特定对象。假设我使用vehicleId(5bebf4561708fd40ea4af125)和menu(brakeWheel)搜索一个车辆属性,那么它仅返回那些包含菜单menu(brakeWheel)的属性对象。谢谢大家。

MongoDB数据

 {
        "_id": "5bebf4561708fd40ea4af125",
        "category": "Bike",
        "name": "Pulsar 150 extreme",
        "brand": "Bajaj",
        "releseDate": "2018-08-10T18:00:00.000Z",
        "price": 150000,
        "otherProperties": [
                {
                    "_id": "5bf3d8f806c71747bd349f04",
                    "name": "fuel_efficiency_range",
                    "value": "fixed",
                    "menu": "feuelEfficiency"
                },
                {
                    "_id": "5bf3d8db06c71747bd349f03",
                    "name": "front_break_type",
                    "value": "fixed",
                    "menu": "brakeWheel"
                },
                {
                    "_id": "5bf3d8cc06c71747bd349f02",
                    "name": "front_break_size",
                    "value": "fixed",
                    "menu": "brakeWheel"
                },
                {
                    "_id": "5bf3d8b606c71747bd349f01",
                    "name": "calliper_type",
                    "value": "fixed",
                    "menu": "brakeWheel"
                },
                {
                    "_id": "5bf3d1f61d55494126ba0877",
                    "name": "chassis_type",
                    "value": "fixed",
                    "menu": "dimensionChassis"
                }
            ]
    }

我想得到

{
_id: 5bebf4561708fd40ea4af125,
"otherProperties": [
        {
            "_id": "5bf3d8db06c71747bd349f03",
            "name": "front_break_type",
            "value": "fixed",
            "menu": "brakeWheel"
        },
        {
            "_id": "5bf3d8cc06c71747bd349f02",
            "name": "front_break_size",
            "value": "fixed",
            "menu": "brakeWheel"
        },
        {
            "_id": "5bf3d8b606c71747bd349f01",
            "name": "calliper_type",
            "value": "fixed",
            "menu": "brakeWheel"
        }

   ]
}

0 个答案:

没有答案