Search for a record in mongoDB by a ruc number

0

I need to look for a record in mongoDb by the NUMERO_RUC using node.js, try for findById and it works perfectly but I want to search for other data that is not the id, you can help me I do not know where my fault is.

Method to obtain a record:

//Obtener registros con el ruc
app.get('/personas/:ruc', function(req, res) {
    rucs.findById(req.params.ruc, function(error, persona) {
   if (error) return res.status(500).send(error);

   res.json(persona);
  });
});

This is the record in MongoDB:

{
"_id": "5b82eaac31e4182794de57d3",
"NUMERO_RUC": "1703467785001",
"RAZON_SOCIAL": "BASANTES JARA MARIANA DE JESUS",
"NOMBRE_COMERCIAL": "",
"ESTADO_CONTRIBUYENTE": "ACTIVO",
}

I'm doing tests with Postman and I get the following error when I want to get the record through the ruc:

"message": "Cast to ObjectId failed for value \"1703467785001\" at path 
\"_id\" for model \"rucs\"",
"name": "CastError",
"stringValue": "\"1703467785001\"",
"kind": "ObjectId",
"value": "1703467785001",
"path": "_id"

}

    
asked by Andres 02.09.2018 в 21:57
source

1 answer

0

I imagine that by the method .findById() you are using mongoose .

.findById() receives as parameter a _id (type ObjectID), so if you want to search for another field you can use the .findOne() method where you pass an object with the fields you want to filter.

Anyway, take a look at the documentation of findById () and findOne ()

Ex:

rucs.findOne({ NUMERO_RUC: req.params.ruc }, function(error, persona) {
   if (error) return res.status(500).send(error);

   res.json(persona);
});
    
answered by 03.09.2018 / 20:31
source