1

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

Heru Wijayanto
  • 439
  • 3
  • 5
  • 16
  • You can try this way https://stackoverflow.com/questions/53866016/nodejs-callback-after-multiple-async-functions-in-for-loop/53866377#53866377 – Ashh May 20 '19 at 06:54
  • so if possible I know what to edit if using a lookup? can give me some clue.. – Heru Wijayanto May 20 '19 at 07:07
  • If You post some initial sample collection and the expected output then I can help you. – Ashh May 20 '19 at 12:53
  • like that sir? pls help me.. i'm stuck – Heru Wijayanto May 21 '19 at 01:59
  • I need both sample collections and the output in **Json** format. Images do not work here. It would be easier for us to just copy and paste the documents and produce the output. – Ashh May 21 '19 at 06:18
  • this is sir.. [link](https://ufile.io/x6bmihf4) hopefully as you expect – Heru Wijayanto May 21 '19 at 06:42
  • No man. Just remove all the content from your above question. Copy and paste sample data of your collections and paste an expected output as I [do](https://stackoverflow.com/questions/49953780/lookup-multiple-levels-without-unwind). – Ashh May 21 '19 at 06:49
  • is it clear sir? i hope, i do as you expect.. – Heru Wijayanto May 21 '19 at 07:14
  • I see you expected output has following keys `"role": "admin", "address": "singapore", "age": "12", "hobby": "cooking", "speciality": "sleeping"` where they comes from? – Ashh May 21 '19 at 07:19
  • it was obtained from the meta account, I took the id on the account and filtered it on the meta account ... then with account_id that was entered with _id from the account ... I joined it ... looked at from my controller – Heru Wijayanto May 21 '19 at 07:22

0 Answers0