0

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

1 Answer 1

1

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']]}}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.