Last entry

Asked

Viewed 1,375 times

0

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

I’m doing a search, comparing two IDS, need only return 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 doesn’t work.

1 answer

3

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

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

Or

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

That is to say, 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) {
     // (...)
},
  • 1

    Okay, good. I’ll delete the comments then...

Browser other questions tagged

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