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

155
Visualizações
MondoDB filter based on array of objects

I have these type of data on a Collection:

{
  "_id": {
    "$oid": "6014662b3a38ba4929b602ab"
  },
  "collection": [
    {
      "name": "Facility",
      "_id": {
        "$oid": "601466293a38ba4929b602aa"
      },
      "is_deleted": false
    },
    {
      "name": "User",
      "_id": {
        "$oid": "60021529c02fa2b27e7c1989"
      },
      "is_deleted": false
    },
    {
      "name": "Asset",
      "_id": {
        "$oid": "6018948fc35a109224439f73"
      },

    },
    {
      "name": "Asset",
      "_id": {
        "$oid": "604a734fca10c74d800031db"
      },
      "is_deleted": true
    }
  ],
  "customer": "TestCustomer",
  "is_deleted": false
}

And also I have this query


db.getCollection("Collection").aggregate([{
    '$match': {
        '$and': [{
            'collection.name': 'User',
            'collection._id': ObjectId('60021529c02fa2b27e7c1989'),
            'collection.is_deleted': false
        }, {
            'collection.name': 'Asset',
            'collection._id': ObjectId('604a734fca10c74d800031db'),
            'collection.is_deleted': false
        }],
        'customer': 'TestCustomer',
        'is_deleted': false
    }
}])

is_deleted on Asset is true in the database, but I am requesting when the value is false.

When this query is executed, is not supposed to return nay data, but it does. I have been trying to resolve this issue but nothing has helped so far.

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

0

Because you're trying to match elements inside an array, you should use $elemMatch instead. Also there is no need for the $and operator.

db.collection.aggregate([
  {
    "$match": {
      "collection": {
        "$elemMatch": {
          "name": "User",
          "_id": ObjectId("60021529c02fa2b27e7c1989"),
          "is_deleted": false
        }
      },
      "collection": {
        "$elemMatch": {
          "name": "Asset",
          "_id": ObjectId("604a734fca10c74d800031db"),
          "is_deleted": false
        }
      },
      "customer": "TestCustomer",
      "is_deleted": false
    }
  }
])

Playground: https://mongoplayground.net/p/cdz9TgQ8HNw

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