Last record

0

I need to know how to get the last record stored in the mongo.

I'm doing a search, comparing two IDS, I need to return only the last record.

I have the following variables:

client.name  
client.cpf  
client.endereço

I'm doing the search as follows:

Client.findById({$and: [{_id: client.Client.id}, {_id: contact.id}]}, {sort: {cpf: -1 }}, function(err, clients) {
                  console.log("client.id: "+client.Client.id)
                  console.log("post"+post)
                  console.log("imprimir: "+contact.code)
                  console.log("nome"+client.cpf)
                callback();
              });
            },

But it does not work.

    
asked by anonymous 24.06.2015 / 16:18

1 answer

3

Nodejs , if it is Mongoose 3.8+, you can use the syntax:

model.findOne().sort({ field: 'asc', _id: -1 })

Or

model.findOne().sort({ field: -_id })

That is, cpf -1 does not seem to satisfy the order, use a "decrementable" field as ID or dates, since the CPF does not follow an order.

(...) {sort: {id: -1 }} (...)

Example

Client.findById({$and: [{_id: client.Client.id}, {_id: contact.id}]}, {sort: {client.Client.id: -1 }}, function(err, clients) {
     // (...)
},
    
24.06.2015 / 16:55