Unable to use Model.findbyId in mongoose

I am using MongoDB along with nodejs, expressjs, ejs and passport. I have setup the MVC and am sending a dynamic URL from views.

This is my MongoDB schema:

const companySchema = new mongoose.Schema({
    country: {
        type: String,
    username: {
        type: String,
        unique: true,
    password: {
        type: String,
    vaccine: {
        name: String,
        price: Number,
}, {
    timestamps: true

From views I have sent a URL which has this format /vaccine/{vaccine.name}, where vaccine.name is the data in the schema. On serverside i am slicing the URL to get the exact vaccine name. This is my query to get the user:

const Company = require("../models/company");

module.exports.transaction_req = function (req, res) {
    if (req.isAuthenticated()) {
        const req_url = req.url.slice(1);
        // console.log(req_url);
        Company.findOne({vaccine.name: req_url}, function(err, user){
        return res.render('transaction');

    return res.redirect('/login');

It is giving this error saying that I cannot use ‘.’ in query:

Company.findOne({vaccine.name: req_url}, function(err, user){
SyntaxError: Unexpected token '.'

What is the alternative to get user with that given vaccine.name?


You cannot use property accessor in query, wrap your vaccine.name into quotes, as 'vaccine.name'.

Leave a Reply

Your email address will not be published. Required fields are marked *