Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

329
Visualizações
Mongoose: Check if value exists in string array

I am querying to get all articles that have a specific source name and category.

I have an article schema that looks like below.

The issue I'm facing is that, it fetches the articles with the correct source name, but it doesn't fetch for the right category. For example, if source is "cnn" and category is "sports", it fetches all articles from CNN correctly, but not with correct categories (the articles may have a categories of ['politics', 'culture'] with no sports)

const article_schema = new mongoose.Schema({
    title: {
        type: String, 
        required: true
    },
    thumbnail_url: {
        type: String, 
        required: true
    },
    summary: {
        type: String, 
        required: true
    },
    text: {
        type: String, 
        required: true
    },
    link: {
        type: String, 
        required: true
    },
    publish_date: {
        type: String, 
        required: true
    },
    source: {
        name: {
            type: String, 
            required: true
        },
        name_slug: {
            type: String, 
            required: true
        },
        link: {
            type: String, 
            required: true
        },
        display_picture_url: {
            type: String, 
            required: true
        },
        biography: {
            type: String, 
            required: true
        },
        tags: {
            type: [String], 
            required: true
        },
        rssFeeds: [{
            url: {
                type: String, 
                required: true
            },
            categories: {
                type: [String], 
                required: true
            }
        }]
    }
}); 
let getNews = async (req, res) => {
    const { sort, filter, current_page, limit, category_slug } = req.body;
    
    let articles = await Article.findOne({ 'source.name_slug': filter, categories: category_slug });

    res.setHeader('Content-Type', 'application/json');
    res.status(200).send({ articles });
} 
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda