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

255
Visualizações
Mongo-go-driver nested query golang

I used to have two filters to get data from my mongoDB, however I do not think that it is efficient considering it has to do two queries to the DB.


    filter = bson.M{
        "$and": []bson.M{
            {"partnerA.id": id},
            {"unlocked": false},
            {"deletedAt": nil},
        },
    }

    filter = bson.M{
        "$and": []bson.M{
            {"partnerB.id": id},
            {"unlocked": false},
            {"deletedAt": nil},
        },
    }

I tried to combine them using this solution I found and came out with this filter:

    filter := bson.M{
        "$and": []bson.M{
            {"partnerA.id": id},
            {"unlocked": false},
            {"deletedAt": nil},
        },
        "$or": bson.A{
            bson.M{"$and": []bson.M{
                {"partnerB.id": id},
                {"unlocked": false},
                {"deletedAt": nil},
            }},
        },
    }

However it does not work and I can't find the solution for it. Does anyone see the problem for this?

Thank you.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I presume you are trying to combine those two queries with OR operator. Another thing, I saw two similar clauses between the two, it's "unlocked": false and "deletedAt": nil.

You can have shorter query like below:

filter := bson.M{
    "$or": []bson.M{
        {"partnerA.id": id},
        {"partnerB.id": id},
    },
    "unlocked": false,
    "deletedAt": nil,
}

Update #1

How about a new query where I only return the values if ((partnerA.id = id and partnerA.unlocked = true) or (partnerB.id = id and partnerB.unlocked = true)) Blockquote

filter := bson.M{
    "$or": []bson.M{
        {
            "partnerA.id": id,
            "partnerA.unlocked": true,
        },
        {
            "partnerB.id": id,
            "partnerB.unlocked": true,
        },
    },
}
over 4 years ago · Santiago Trujillo Relatório
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