如何在不填充猫鼬的情况下将2个集合加入1个

时间:2019-05-20 06:52:36

标签: javascript node.js mongodb mongoose

所以我的任务是将2个集合合并为1个,以便能够通过其他参数查询进行搜索 像ac_key或ac_value,

但是现实情况并不像预期的那样,这仅适用于一个集合,当我对另一个集合进行搜索时什么也没发生

帐户收款

{
    "_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集合

{
    "_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"
}

预期产量

[{
        "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"
    }]

条件:我想用参数ac_key和ac_value查找键和值,当我查找非特定的和相关的数据时,它不会出现,并获取与同一个account_id相关的所有元数据

假设要从帐户合并到元帐户的数据存在_id(帐户)= account_id(帐户_meta)

实际上,当我按ac_key:age和ac_value:'12'时,我得到:

[
    {
        "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"
    }
]

这是我的控制器

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))
};

预先感谢

0 个答案:

没有答案