Search Mongodb Array

Asked

Viewed 1,030 times

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 ?

  • reply with elemMatch solved?

3 answers

3

  • he mMatch solved?

2

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

Assuming the BD name is "test", the command db.test.find() 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? Then the query db.test.find({STATUS_CHAMADO:'aberto'}) would be the consultation you wanted.

If you only want one document, just exchange the command for findOne.

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

Source

0

Good afternoon William, it was not very clear what you need, but I will send you two forms, one of them should suit you.

For this type of question, I prefer to use Aggregate, I am searching based on the history ID you want. In the first query, I am returning all the information of certain history:

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

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

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

I hope I’ve helped.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.