Currently I have this query:
Post.find().populate([
{
path: 'page',
populate: {
path: 'url',
populate: {
path: 'i18n',
match: {
locale: 'es_ES'
}
}
}
}
]);
as result, I get:
{
"title": "Title Post",
"page": {
"title": "Title Page",
"url": [
{
"url": "/english",
"i18n": null
},
{
"url": "/spanish",
"i18n": {
"name": "Spanish",
"iso": "es",
"locale": "es_ES",
}
}
]
}
}
and url
is filtered after query:
let url = posts.page.url.filter(value => {
return value.i18n;
});
I want to load only url
relations with i18n.locale = es_ES
condition but I don't know how to do it. Wanted result:
{
"title": "Title Post",
"page": {
"title": "Title Page",
"url": [
{
"url": "/spanish",
"i18n": {
"name": "Spanish",
"iso": "es",
"locale": "es_ES",
}
}
]
}
}
I have tried:
Post.find().populate([
{
path: 'page',
populate: {
path: 'url',
match: {
'i18n.locale': 'es_ES'
}
}
}
]);
But is not a valid filter.
Models:
module.exports = mongoose.model('i18n', new Schema({
name: String,
iso: String,
locale: String
}));
module.exports = mongoose.model('page', new Schema({
title: Object,
url: [{
type: Schema.Types.ObjectId,
ref: 'url'
}]
));
module.exports = mongoose.model('post', new Schema({
title: Object,
page: {
type: Schema.Types.ObjectId,
ref: 'page'
}
}));
module.exports = mongoose.model('url', new Schema({
url: String,
i18n: {
type: Schema.Types.ObjectId,
ref: 'i18n'
}
}));
I'm using MongoDB server version 3.6.6 and mongoose 5.2.6.
Thanks!