adrigm
1
How can I filter by an array? For example, I have a field for category and I can do:
const body = {
filter: {
published: true,
'category.display': 'MyCategory',
},
limit: 10,
sort: { _created: -1 },
simple: 1
};
But, I can’t do this for an array. The only way I have found is to write the index directly, but I want it to search all the elements of the array.
const body = {
filter: {
published: true,
'tags.0.display': 'MyTag',
},
limit: 10,
sort: { _created: -1 },
simple: 1
};
Is there any way to filter the entries looking in an array?
wuh
2
An old post here suggests this should be possible using:
filter: {
tags: {
$all: ["tag1", "tag2"]
}
}
However, I just tested it and it doesn’t work
Maybe Artur will see this and help.
you need to use something like below:
{
"filter": {
"tags": {
"$in": ["tag1", "tag2"]
}
}
}
adrigm
4
Hi again
And for collectionlink arrays? This is a objects’ array. I want filter by _id
tags: Array(4)
0: {_id: "5d3a095a6566621e5200009b", link: "tag", display: "RxJs"}
1: {_id: "5d3a09666566621e54000170", link: "tag", display: "Angular"}
2: {_id: "5d3a09706566621e5200002f", link: "tag", display: "Firebase"}
try the following:
{
"filter": {
"tags._id": {
"$in": ["tag1", "tag2"]
}
}
}
adrigm
6
I tried but it didn’t work.
I think this is because tags is the array that contain objects that contain “_id” attribute.
if you have a collection with a field (collectionlink) named tags it shall work, e.g.:
5c221c99164bfc00505a580f => Collection entry “Tag 1”
{
"populate": 0,
"simple": 1,
"filter": {
"tags._id": {
"$in": ["5c221c99164bfc00505a580f"]
}
}
}
[
{
"_id": "5d3cc660319f7500eb75c5d2",
"title": "Page 1",
"tags": [
{
"_id": "5c221c99164bfc00505a580f",
"link": "tags",
"display": "Tag 1"
},
{
"_id": "5c221c99164bfc005441304d",
"link": "tags",
"display": "Tag 2"
}
]
},
{
"_id": "5d3cc665319f7500eb75c5d4",
"title": "Page 2",
"tags": [
{
"_id": "5c221c99164bfc00505a580f",
"link": "tags",
"display": "Tag 1"
}
]
}
]
adrigm
8
I really tried it and it doesn’t work.
The only solution I have found for now is to search in each position of the array, since I have my posts limited to 4 tags.
Although this is not a definitive solution, but it works for me:
public getPostsByTagID(id: string) {
const body = {
filter: {
published: true,
$or: [{ 'tags.0._id': id }, { 'tags.1._id': id }, { 'tags.3._id': id }, { 'tags.4._id': id }]
},
sort: { _created: -1 },
limit: this.postsPerPages,
simple: 1,
populate: 1
};
return this.http.post<Post[]>(this.urlPost, body, { headers: this.headers });
}
c
linhous
9
I’m going through the same situation, although I didn’t want to have to limit the amount of “tags”.
Is there still no alternative? Just limiting it?