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?