I want to perform a query on a mongo database with three variables: age, job, and location as an option. I use Node and Express on the server side.
I have written this:
router.post("/search", async (req, res) => {
try {
const { jobs, minAge, maxAge, location, maxDistance } = req.body;
// jobs = ["Tech", "Sales"], minAge=45, maxAge = 62, location=[1.23, 4.56], maxDistance=10000
if (!location) {
const user = User.aggregate([
{$match: {
jobs: { $or: jobs },
age: { $gte: minAge, $lte: maxAge },
}},
]).lean()
return res.json({user})
});
}
const user = User.aggregate([
{ $match: {
job: { $or: job },
age: { $gte: minAge, $lte: maxAge }
},
$geoNear: {
near: { type: "Point", coordinates: location.coordinates },
distanceField: "dist.calculated",
maxDistance,
},
},
]).lean();
return res.json({ user });
} catch (err) {
return res.json({ errorType: "search" });
}
});
In case it might be helpful, here is the User Schema:
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const userSchema = new Schema(
{
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
age: { type: Number, min: 18 },
job: {
type: String,
enum: ["Tech", "Product", "Marketing", "Sales", "HR", "Other"],
},
location: {
type: { type: String, enum: ["Point"] },
coordinates: { type: [Number] },
},
},
{ timestamps: true }
);
userSchema.index({ age: 1, job: 1, location: "2dsphere" });
module.exports = mongoose.model("User", userSchema);
The error is MongoError: unknown operator: $or. How to properly retrieve an array of users that match the conditions expressed in the query?