I know maybe this question has something in common with other questions but after I followed and tried, I still could not find the answer, so just get to the point.
i have output like this:
[
{
"status": "not verified",
"metas": [
{
"value": "android",
"_id": "5ce4eba8b697b12b14e4d94a",
"key": "smartphone"
}
],
"_id": "5ce2407c99c7952d4c6f0f59",
"name": "Monkey D Garp",
"phone": "1234",
"email": "bbb@bbb.com"
},
{
"status": "not verified",
"metas": [
{
"value": "volley",
"_id": "5ce5fcf666bb1330a48e8f7b",
"key": "sport"
},
{
"value": "asus",
"_id": "5ce5fd0966bb1330a48e8f7c",
"key": "laptop"
}
],
"_id": "5ce2408b99c7952d4c6f0f5b",
"name": "Monkey D Garp",
"phone": "1234",
"email": "aaa@aaa.com"
},{
"status": "not verified",
"metas": [
{
"value": "samsung",
"_id": "5ce5fcf666bb1330a48e8f8a",
"key": "laptop"
},
{
"value": "huawei",
"_id": "5ce5fd0966bb1330a48e8f8b",
"key": "smartphone"
}
],
"_id": "5ce2408b99c7952d4c6f0d6c",
"name": "Monkey D Law",
"phone": "1234",
"email": "ccc@ccc.com"
}
]
expected output when i search value: asus
[{
"status": "not verified",
"metas": [
{
"value": "volley",
"_id": "5ce5fcf666bb1330a48e8f7b",
"key": "sport"
},
{
"value": "asus",
"_id": "5ce5fd0966bb1330a48e8f7c",
"key": "laptop"
}
],
"_id": "5ce2408b99c7952d4c6f0f5b",
"name": "Monkey D Garp",
"phone": "1234",
"email": "aaa@aaa.com"
}]
and when i search value_else: asus.. i expected:
[
{
"status": "not verified",
"metas": [
{
"value": "android",
"_id": "5ce4eba8b697b12b14e4d94a",
"key": "smartphone"
}
],
"_id": "5ce2407c99c7952d4c6f0f59",
"name": "Monkey D Garp",
"phone": "1234",
"email": "bbb@bbb.com"
},
{
"status": "not verified",
"metas": [
{
"value": "samsung",
"_id": "5ce5fcf666bb1330a48e8f8a",
"key": "laptop"
},
{
"value": "huawei",
"_id": "5ce5fd0966bb1330a48e8f8b",
"key": "smartphone"
}
],
"_id": "5ce2408b99c7952d4c6f0d6c",
"name": "Monkey D Law",
"phone": "1234",
"email": "ccc@ccc.com"
}
]
and this is my controller:
exports.get_Account = async (req, res) => {
const { _id, name, email, phone, status, key, value, value_else } = req.query
const accounts = await Account.find({
...(_id && {_id : { $in : _id.split(",") }}),
...(name && {$text : { $search: name }}),
...(email && {email : { $in : email.split(",") }}),
...(phone && {phone : { $in : phone.split(",") }}),
...(status && {status : { $in : status.split(",") }}),
}).populate({path: 'metas', select: 'key value'})
console.log('key', key)
console.log('value', value)
res.json(accounts)
};
hope you can help me guys.. thanks in advance