0

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

Heru Wijayanto
  • 439
  • 3
  • 5
  • 16
  • hello again sir.. when yesterday's help was not resolved, I tried new ways to populate, but there were still obstacles to finding data in nested objects. and add conditions for the appropriate query hehe – Heru Wijayanto May 23 '19 at 06:32
  • You cannot prevent the data from parent collection to being returned while using `.populate` And that is what the duplicate answer explained very well. – Ashh May 23 '19 at 06:56
  • but I did not change the parent data, I changed the data from populate to be filtered with value or value_else – Heru Wijayanto May 23 '19 at 07:12

0 Answers0