Get the value of a document in mongoDB

0

I have a collection where the documents are like this (in MongoDB):

{
_id: 'ABPS1001',
Marca: 'DecNag',
serial: '2393-829-109',
resumen: [
    {
        estado: '1',
        nombresAg: 'Gina',
        apellidosAg: 'Saenz',
        coordinador_id: '1025',
        fechaMov: '25-10-2016 11:33'
    },
    {
        estado: '0',
        tecnicoID: '11',
        coordinador_id: '1025',
        fechaMov: '30-10-2016 16:29',
        idReemplazo: 'ABPS1026'
    },
    {
        estado: '1',
        nombresAg: 'Diana',
        apellidosAg: 'Gutierrez'
        coordinador_id: '1014',
        tecnico: '10',
        fechaMov: '04-11-2016 09:12'
    },
    {
        estado: '0',
        tecnicoID: '12',
        coordinador_id: '1014',
        fechaMov: '30-11-2016 16:25',
        idReemplazo: 'ABPS1021'
    },
    {
        estado: '1',
        nombresAg: 'Laura',
        apellidosAg: 'Diaz',
        coordinador_id: '1012',
        tecnico: '11',
        fechaMov: '04-12-2016 11:33'
    },
    {
        estado: '0',
        tecnicoID: '10',
        coordinador_id: '1012',
        fechaMov: '22-12-2016 12:21'
    },
    {
        estado: '1',
        nombresAg: '172.27.48.125',
        apellidosAg: ''
        coordinador_id: '1004',
        tecnico: '12',
        fechaMov: '27-12-2016 08:30',
        idReemplazo: 'ABPS1035'
    },
    {
        estado: '0',
        tecnicoID: '11',
        coordinador_id: '1004',
        fechaMov: '02-02-2017 14:12'
    }
 ]
 }

I need to get the last "summary" entry of the document with _id: 'ABPS1001' and not the entire object. Is there any way to do it with the MongoDB statements instead of processing it with the programming language?

Thanks in advance.

    
asked by MHG 26.10.2016 в 15:44
source

2 answers

0

According to the official documentation , adapted to your case:

db.nombre_coleccion.find( { _id: "ABPS1001" }, { resumen: 1} )

If you do not want the _id field:

db.nombre_coleccion.find( { _id: "ABPS1001" }, { resumen: 1, _id: 0 } )

    
answered by 26.10.2016 в 23:54
0

It can be with $ slice . Your query may be like this:

db.collection.find({_id: "ABPS1001"}, {resumen: {$slice: -1}}).pretty()

The result brings only the last element of the array.

    
answered by 28.10.2016 в 06:34