so I have the task to combine 2 collections into 1 to be able to search by additional parameter queries like ac_key or ac_value,
but the reality is not as expected, this only works for one collection, when I do a search with the other collection nothing happens
account collection
{
"_id" : ObjectId("5ce2409399c7952d4c6f0f5d"),
"status" : "not verified",
"name" : "Monkey D Garp",
"phone" : "1234",
"email" : "ccc@ccc.com",
"password" : "$2a$10$186wQau8GBtqOORovWP7..r8bwSAW1kK9Cb0lT8ckeNFSkEDYjOuu"
},
{
"_id" : ObjectId("5ce2408b99c7952d4c6f0f5b"),
"status" : "not verified",
"name" : "Monkey D Garp",
"phone" : "1234",
"email" : "aaa@aaa.com",
"password" : "$2a$10$WskmjNldC2TQ13Rl6ZLqROJwIux1KwM2tkCqfbiMSxWKRUAgsQWn."
},
{
"_id" : ObjectId("5ce2407c99c7952d4c6f0f59"),
"status" : "not verified",
"name" : "Monkey D Garp",
"phone" : "1234",
"email" : "bbb@bbb.com",
"password" : "$2a$10$g1WRwu4Tp85hIIyw4ONd9e3CGOd7u8UN1jfF.zsVpAOE9Usdy01Bm"
}
account_meta collection
{
"_id" : ObjectId("5ce37884551b0b07f4b60598"),
"value" : "sleeping",
"key" : "speciality",
"account_id" : ObjectId("5ce2407c99c7952d4c6f0f59")
},
{
"_id" : ObjectId("5ce240fc99c7952d4c6f0f61"),
"value" : "cooking",
"key" : "hobby",
"account_id" : ObjectId("5ce2407c99c7952d4c6f0f59")
},
{
"_id" : ObjectId("5ce240f399c7952d4c6f0f60"),
"value" : "12",
"key" : "age",
"account_id" : ObjectId("5ce2407c99c7952d4c6f0f59")
},
{
"_id" : ObjectId("5ce240e799c7952d4c6f0f5f"),
"value" : "singapore",
"key" : "address",
"account_id" : ObjectId("5ce2407c99c7952d4c6f0f59")
},
{
"_id" : ObjectId("5ce2409399c7952d4c6f0f5e"),
"value" : "staff",
"account_id" : ObjectId("5ce2409399c7952d4c6f0f5d"),
"key" : "role"
},
{
"_id" : ObjectId("5ce2408b99c7952d4c6f0f5c"),
"value" : "user",
"account_id" : ObjectId("5ce2408b99c7952d4c6f0f5b"),
"key" : "role"
},
{
"_id" : ObjectId("5ce2407c99c7952d4c6f0f5a"),
"value" : "admin",
"account_id" : ObjectId("5ce2407c99c7952d4c6f0f59"),
"key" : "role"
}
expected output
[{
"status": "not verified",
"_id": "5ce2407c99c7952d4c6f0f59",
"name": "Monkey D Garp",
"phone": "1234",
"email": "bbb@bbb.com",
"password": "$2a$10$g1WRwu4Tp85hIIyw4ONd9e3CGOd7u8UN1jfF.zsVpAOE9Usdy01Bm",
"role": "admin",
"address": "singapore",
"age": "12",
"hobby": "cooking",
"speciality": "sleeping"
}]
the condition: I want to find the key and value with the parameters ac_key and ac_value, when I look for non-specific and related data it will not appear, and get all the meta related to the same account_id
assuming data to merge from account to meta account exists _id (account) = account_id (account_meta)
in reality when i hit ac_key: age, and ac_value: '12', i get:
[
{
"status": "not verified",
"_id": "5ce2407c99c7952d4c6f0f59",
"name": "Monkey D Garp",
"phone": "1234",
"email": "bbb@bbb.com",
"password": "$2a$10$g1WRwu4Tp85hIIyw4ONd9e3CGOd7u8UN1jfF.zsVpAOE9Usdy01Bm",
"role": "admin",
"address": "singapore",
"age": "12",
"hobby": "cooking",
"speciality": "sleeping"
},
{
"status": "not verified",
"_id": "5ce2408b99c7952d4c6f0f5b",
"name": "Monkey D Garp",
"phone": "1234",
"email": "aaa@aaa.com",
"password": "$2a$10$WskmjNldC2TQ13Rl6ZLqROJwIux1KwM2tkCqfbiMSxWKRUAgsQWn.",
"role": "user"
},
{
"status": "not verified",
"_id": "5ce2409399c7952d4c6f0f5d",
"name": "Monkey D Garp",
"phone": "1234",
"email": "ccc@ccc.com",
"password": "$2a$10$186wQau8GBtqOORovWP7..r8bwSAW1kK9Cb0lT8ckeNFSkEDYjOuu",
"role": "staff"
}
]
this is my controller
exports.get_Account = async (req, res) => {
const { _id, name, email, phone, status, ac_key, ac_value } = req.query
const accounts = await Account.find({
// query database query
...(_id && {_id : { $in : _id.split(",") }}),
...(name && {$text : { $search: name }}),
...(email && {email : { $in : email.split(",") }}),
...(phone && {phone : { $in : phone.split(",") }}),
...(status && {status : { $in : status.split(",") }}),
});
const newAcc = await accounts.map(async account => {
const accMeta = await AccountMeta.find({
...({account_id : account._id}),
...(ac_key && {key : ac_key}),
...(ac_value && {value : ac_value})
});
console.log('accMeta', accMeta)
const new_account = {};
await accMeta.map(editMeta => {
new_account[editMeta.key] = editMeta.value;
});
let dynamicAccount = Object.assign({}, account._doc, new_account); //cuma
return {...dynamicAccount}
});
await Promise.all(newAcc).then(result => res.status(200).json(result))
};
thanks in advance