Solution
Since you are using the MySQL
loopback connector to connect to your MySQL
database, currently this connector treats both String/JSON
as VARCHAR
. As such, you could try the following modification to like
{where: {events: {like: '%id:'+7+'%'}}}
or
const orders = await this.orderRepository.find({
where: {
events: {
like: '%id:'+event.id+'%'
}
}
});
or using regular expressions
const orders = await this.orderRepository.find({
where: {
events: {
regexp: '.*id:'+event.id+'.*'
}
}
});
const orders = await this.orderRepository.find({
where: {
events: {
regexp: new RegExp(".*id:"+event.id+".*")
}
}
});
in an attempt to match the json
pattern {
id:7,name:'Event 7'}
where in this case the value inside id
could be 7
.
Assumptions
Based on your question and the mysql error shown, the following assumptions were made:
Schema (MySQL v5.7)
create table samples(id int primary key auto_increment, events varchar(400));
insert into samples(events) values
('[{id:3,name:\"Boscobel\"},{id:4,name:\"Rays\"}]'),
('[{id:7,name:\"Boscobel 7\"},{id:8,name:\"Rays 8\"}]');
Should Receive Results
Query #1
select * from samples where events like '%id\:7%';
| id | events |
| --- | ----------------------------------------------- |
| 2 | [{id:7,name:"Boscobel 7"},{id:8,name:"Rays 8"}] |
Query #2
select * from samples where events like '%id:7%';
| id | events |
| --- | ----------------------------------------------- |
| 2 | [{id:7,name:"Boscobel 7"},{id:8,name:"Rays 8"}] |
Should Not Receive Results
Query #3
select * from samples where events like '%id\:70%';
There are no results to be displayed.
Query #4
select * from samples where events like '%id:200%';
There are no results to be displayed.
View on DB Fiddle