[SERVER-31055] $in operator complains about var not being an array, if it is Created: 12/Sep/17  Updated: 27/Oct/23  Resolved: 14/Sep/17

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 3.4.3
Fix Version/s: None

Type: Question Priority: Minor - P4
Reporter: Henri-Maxime Ducoulombier Assignee: Mark Agarunov
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

When running a query like that, we have an error "$in operator needs an array", even if the content of $oi expression is an array.

 
db.demo.aggregate(
[
	{"$match":{"_id":{"$in":["aaa","bbb"]}}},
	{"$project":{
		"order_items":1,
		"orders":1,
		"oi":'$order_items.order_id'
	}},
	{
	 "$addFields":{"sz":{"$size":"$oi"}} // This works, $oi is an array and $size works on arrays
	},
	{"$match":{"orders.order_id": {"$in":"$oi"}} // This does not work even if $oi is an array
]
)

I guess this is complicated for query engine to be sure that $oi is an array, and $in operator has been strengthen considering the fact that value must be an array, but is there a way to achieve that ?



 Comments   
Comment by Mark Agarunov [ 14/Sep/17 ]

Hello hmducoulombier@marketing1by1.com,

Thank you for the report. Unfortunately the behavior you've described is not currently supported. As a workaround, you can check orders.order_id membership in $oi via the "$in" aggregation expression and setting a field to filter on if condition returns true.

Thanks,
Mark

Generated at Thu Feb 08 04:25:51 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.