Reputation: 451
I am trying to use $in inside $match condition of aggregate. I have SegmentList as literal array and Segment as a literal value. I am trying to match with $in condition as in query below.
db.collection.aggregate([
{'$match': {
'MaterialNumber': '867522'
}},
{'$project':{
'SegmentList': {'$literal':['A']},
'Segment':{'$literal':'A'},
'value':{'$literal':'B'},
}
},
{'$match': {
'Segment':{'$in':'$SegmentList'}
}
}
])
But I am getting an error.
assert: command failed: {
"ok" : 0,
"errmsg" : "bad query: BadValue: $in needs an array",
"code" : 16810
} : aggregate failed
I am not not able to understand what is problem
Upvotes: 0
Views: 4064
Reputation: 75924
Not sure what you are intending to acheive here as all you are doing comparing if 'A' is in array with element 'A' which is always true.
$match
in its regular form only takes a value not field reference.
No need to use $literal
you can directly pass [].
You can use one of the following query based on mongo version.
3.4 & below:
{'$match': {'Segment':{'$in':['A']}}
3.6 ( not needed for your use case )
{'$match': {'$expr':{'$in':['$Segment', ['A']]}}}
Upvotes: 1