Search MongoDB Array

4

I have these two documents in the mongo:

/* 0 */
{
"_id" : ObjectId("54f395ef7a5a5ea37af77398"),

"HISTORICO" : [],
"MOTIVO" : "",
"RP_CANCELAMENTO" : "",
"DATA_CANCELAMENTO" : null,
"RP_FINALIZACAO" : "",
"DATA_FINALIZACAO" : null,
"STATUS_CHAMADO" : "aberto",
"SOLICITACAO" : "teste.",
"DATA_CHAMADO" : ISODate("2015-03-01T00:00:00.000Z"),
"RP_CHAMADO" : "cristineia evangelista",
"EMAIL_SOLICITANTE" : "",
"TEL_COMERCIAL_SOLICITANTE" : "",
"TEL_RESIDENCIAL_SOLICITANTE" : "(39)00390300_",
"SOLICITANTE" : "Luciana",
"TEL_COMERCIAL_PROPRIETARIO" : "null",
"DDD_PROPRIETARIO" : "38",
"PRIORIDADE" : "media",
"BAIRRO" : "CENTRO",
"CODIGO_IMOVEL" : "03806/2",
"CODIGO_CONTRATO" : "16161",
"__v" : 0
}

/* 1 */
{
"_id" : ObjectId("54f395d47a5a5ea37af77397"),
"RP_ABERTURA" : "guilherme ferreira",
"HISTORICO" : [ 
    [ 
        {
            "DATA_HISTORICO" : "2015-03-01",
            "DADOS_HISTORICO" : "opa",
            "_id" : ObjectId("54f396de7a5a5ea37af77399")
        }
    ], 
    [ 
        {
            "DATA_HISTORICO" : "2015-03-01",
            "DADOS_HISTORICO" : "opa",
            "_id" : ObjectId("54f397437a5a5ea37af7739a")
        }
    ], 
    [ 
        {
            "DATA_HISTORICO" : "2015-03-01",
            "DADOS_HISTORICO" : "teste",
            "_id" : ObjectId("54f39759c8d6e8e17ca7822c")
        }
    ], 
    [ 
        {
            "DATA_HISTORICO" : "2015-03-01",
            "DADOS_HISTORICO" : "teste",
            "_id" : ObjectId("54f397d1c8d6e8e17ca7822d")
        }
    ]
],
"MOTIVO" : "",
"RP_CANCELAMENTO" : "",
"DATA_CANCELAMENTO" : null,
"RP_FINALIZACAO" : "",
"DATA_FINALIZACAO" : null,
"STATUS_CHAMADO" : "aberto",
"SOLICITACAO" : "teste de chaad.",
"DATA_CHAMADO" : ISODate("2015-03-01T00:00:00.000Z"),
"RP_CHAMADO" : "erika ramires",
"EMAIL_SOLICITANTE" : "",
"SOLICITANTE" : "Luciana",
"DDD_PROPRIETARIO" : "38",
"NOME_PROPRIETARIO" : "PAULINA FRANCELINA DOS SANTOS",
"PRIORIDADE" : "media",
"BAIRRO" : "VL GUILHERMINA",
"ENDERECO" : "R LEOBINO CAMARA 55",
"CODIGO_IMOVEL" : "5971",
"CODIGO_CONTRATO" : "14298",
"__v" : 0
}

How do I search for a specific history?

    
asked by anonymous 02.03.2015 / 00:53

3 answers

3

Hello, to search for a specific history, use the $ elemMatch operator:

db.teste.find({HISTORICO: {$elemMatch: {DADOS_HISTORICO:"teste"}}})

mongodb documentation

Similar question in English

    
02.03.2015 / 13:11
2

You use the / query db.<nomeDB>.find() function to do this query.

Assuming the DB name is "test", the db.test.find() command would return all records.

But you want to filter the results, right?

Then the command would be db.test.find({atributo: 'valor'}) . And yes, the value MUST be between ' (apostrophe).

Example : Do you want all documents open? So the query db.test.find({STATUS_CHAMADO:'aberto'}) would be the query you wanted.

If you just want a document, just change the command to findOne .

Example : The query db.test.findOne({STATUS_CHAMADO:'aberto'}) would only return an open document.

Font

    
02.03.2015 / 13:02
0

Good afternoon Guilherme, it was not very clear what you need, but I'll send you two forms, one of them should answer you.

For this type of question, I'd rather use aggregate, I'm doing the search based on the ID of the history you want. In the first query, I am returning all the information of a certain history:

db.teste.aggregate(
[
    { $unwind: "$HISTORICO" },
    { $match: { 'HISTORICO._id': ObjectId('54f39759c8d6e8e17ca7822c') } }
])

In this second query, I only return the Historico information by searching for your ID:

db.teste.aggregate(
[
    { $unwind: "$HISTORICO" },
    { $match: { 'HISTORICO._id': ObjectId('54f39759c8d6e8e17ca7822c') } },
    { $project: { 'HISTORICO': 1, '_id': 0 } }
])

I hope I have helped.

    
10.07.2017 / 22:05