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

77
Visualizações
Querying for an object vs array

How do I query for an object vs an array of objects in a document?

I need to select all the documents that are like Dataset 2.

Dataset 1:

{
    'firstname' : 'John', 
    'lastname': 'Smith', 
    'assistance': [
        {'type': 'Food', 'amount': 20}
    ]
}

Dataset 2:

{
    'firstname' : 'John', 
    'lastname': 'Smith', 
    'assistance': {
        'type': 'Food', 
        'amount': 20
    }
}
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

you can use $type , $type won't work for dataset1, the reason being that it doesn't check if the field is array or not, what it checks is that the field contains array or not.

But if you are looking for dataset 2, you can use $type for object

db.whatever.find( { "assistance" : { $type : 3 } } );

or

db.whatever.find( { "assistance" : { $type : "object" } } );
over 4 years ago · Santiago Trujillo Relatório

0

MongoDB stores data in BSON format.

BSON stands for Binary JSON.

BSON supports various datatypes for values in documents.

According to documentation of MongoDB

$type selects the documents where the value of the field is an instance of the specified BSON type.

According to above mentioned documents

Dataset 1:Datatype of assistance field is an array.

Dataset2 : Datatype of assistance field is an object.

To retrieve only documents containing assistance key having object as a data type please try executing following query in MongoDB shell.

db.collection.find({assistance:{$type:3}})

For more detailed description regarding BSON types please refer documentation as mentioned in below URL

https://docs.mongodb.com/manual/reference/bson-types/

over 4 years ago · Santiago Trujillo Relatório

0

db.foo.find( { "assistance" : { $type : 3 } } ); // 3 is bson type for object

will return both the documents and

db.foo.find( { "assistance" : { $type : 4 } } ); // 4 is bson type for object

will return none of the two documents.

This is becuase in mongodb, when querying on an array, the array elements are checked instead on the complete array.

You can eliminate all elements where assistance is of type array by:

db.foo.find({"assistance.0" : {$exists : false}})
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