我知道这个问题可能与其他问题有共同点,但在我尝试并尝试之后,我仍然找不到答案,所以直截了当。
我有这样的输出:
[
{
"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"
}
]
我搜索值: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"
}]
当我搜索value_else:asus ..我期望:
[
{
"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"
}
]
这是我的控制器:
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)
};
希望您能帮助我。.预先感谢