Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
Pass a plain text in a query value instead of objectId in mongoose

I want to pass an plain text value instead of ObjectId in a query string like this: 127.0.0.1:3000/trips?from=mumbai&to=ahmedabad&date=2022-06-02.

Instead of 127.0.0.1:3000/trips?from=6295f0986f9e32990d8b3488&to=6295f0c06f9e32990d8b348b&date=2022-06-02.

Here is the Location Schema.

const locationSchema = new mongoose.Schema({
    location: {
      name: {
        type: String,
        unique: true,
        required: true,
        lowercase: true,
      },
      subLocation: [String],
    },
  },
  {
    timestamps: true,
  });

I have two different collections in the Location table One is for Mumbai and the other is for Ahmedabad and now I am referencing both of them in the route table like this.

const routeSchema = new mongoose.Schema(
  {
    Location: {
      from: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Location",
        required: true,
      },
      to: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Location",
        required: true,
      },
    }
);

First of all will this work?

In the route table --> In from field I am passing Mumbai's id which is 1 and in the to field I am passing Ahmedabad's id is 2 (as an example it is not the real value) which both are coming from the location table.

When I am passing the id of Mumbai and Ahmedabad 1 and 2 respectively it is working but whenever I pass only Mumbai and Ahmedabad as a plain text then it is throwing an error: Cast to ObjectId failed for value "Mumbai" (type string) at path "Location. to" for model "Route"

here is the GET request:

router.get("/trips", async (req, res) => {
  if (!req.query.from || !req.query.to || !req.query.date) {
    return res.send({
      error: "Please enter the data to get the trip",
    });
  }
  const { from, to, date } = req.query;

  const routes = await Route.find({
    "Location.from": from,
    "Location.to": to,
    date,
  }).populate({
      path: "Location.from Location.to",
      select: "-location.subLocation -_id -createdAt -updatedAt -__v",
    }).select(["-_id", "-busId", "-createdAt", "-updatedAt", "-__v"]);

  return !routes ? res.status(500).send() : res.status(200).send(routes);
});

I want to pass a plain text as a query value, not an objectId.

Is there any way to do it?

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda