问题
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