I have a json object like so:
{
_id: "12345",
identifier: [
{
value: "1",
system: "system1",
text: "text!"
},
{
value: "2",
system: "system1"
}
]
}
How can I use the XDevAPI SearchConditionStr to look for the specific combination of value + system in the identifier array? Something like this, but this doesn't seem to work...
collection.find("'${identifier.value}' IN identifier[*].value && '${identifier.system} IN identifier[*].system")
By using the
INoperator, what happens underneath the covers is basically a call toJSON_CONTAINS().So, if you call:
What gets executed, in the end, is (simplified):
In this case, both those conditions are true, and the document will be returned.
The atomic unit is the document (not a slice of that document). So, in your case, regardless of the value of
valueand/orsystem, you are still looking for the same document (the one whose_idis'12345'). Using such a statement, the document is either returned if all search values are part of it, and it is not returned if one is not.For instance, the following would not yield any results:
EDIT: Potential workaround
I don't think using the CRUD API will allow to perform this kind of "cherry-picking", but you can always use SQL. In that case, one strategy that comes to mind is to use
JSON_SEARCH()for retrieving an array of paths corresponding to each value in the scope ofidentifier[*].valueandidentifier[*].systemi.e. the array indexes and useJSON_OVERLAPS()to ensure they are equal.In this case, the result set will only include documents where the
identifierarray contains at least one JSON object element wherevalueis equal to'2'andsystemis equal tosystem1. The filter is effectively applied over individual array items and not in aggregate, like on a basicINoperation.Disclaimer: I'm the lead developer of the MySQL X DevAPI Connector for Node.js