I have a mongodb query and I want to find the exact match word “Approved”

こ雲淡風輕ζ 提交于 2020-04-30 06:41:05

问题


I have a mongodb query and I want to find the exact match word "Approved" in Array named "Sales.Action".

But the other "Sales.Action" array has the value "Approve" which is also returning.

I have tried $elemMatch but still it is not working.

Here is my json:

{
"Sales" : [
    {
        "Action" : [
            "Approved"
        ],
        "Log" : [
            "Created on: 04/20/2020 42:04:04"
        ]
    },
    {
        "Action" : [
            "Approve",
            "Decline",
            "Edit",
            "Remarks"
        ],
        "Log" : [
            "Created on: 04/20/2020 42:04:04"
        ]
    },
    {
        "Action" : [
            "Approve",
            "Decline",
            "Edit",
            "Remarks"
        ],
        "Log" : [
            "Created on: 04/21/2020 412:04:04"
        ]
    },
    {
        "Action" : [
            "Approve",
            "Decline",
            "Edit",
            "Remarks"
        ],
        "Log" : [
            "Created on: 04/21/2020 412:04:04"
        ]
    },
    {
        "Action" : [
            "Approve",
            "Decline",
            "Edit",
            "Remarks"
        ],
        "Log" : [
            "Created on: 04/23/2020 44:04:04"
        ]
    }
]

}

But I need only one to return.


回答1:


try this code i think this works for you

db.getCollection('your_collection').aggregate([
  { $unwind: '$Sales' },
  {
    $match: {'Sales.Action': 'Approve' }
  },
])


来源:https://stackoverflow.com/questions/61422641/i-have-a-mongodb-query-and-i-want-to-find-the-exact-match-word-approved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!