skip to Main Content

I would like to use Mongoose to find a record of data in MongoDB Atlas. The aqlLevel and batchSize is the value that already know, but inspectionLevel, lotSizeMin and lotSizeMax is the field of one of the table in MongoDB Atlas. I’ve tried the syntax that without where condition and it works. However, with the where condition I can’t get anything back from the table in MongoDB Atlas. Can anyone help me with the Mongoose syntax so that I can find the data with where condition?

The piece of code that I would like to use to search

let aqlLevel = find.aqlLevel;
let batchSize = find.batchSize;
let findSampleSizeCodeLetter = await SampleSize.find({inspectionLevel: aqlLevel})
                                                   .where(batchSize).gte({lotSizeMin}).lte({lotSizeMax}).exec();

The schema of the table is like this below:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const schema = new Schema({
    lotSizeMin: { type: Number},
    lotSizeMax: { type: Number},
    inspectionLevel: {type: String},
    sampleSizeCode: {type: String}
}, { collection : 'SampleSizeCodeLetter' });


schema.set('toJSON', {
    virtuals: true,
    versionKey: false,
    transform: function (doc, ret) {
        delete ret._id;
        delete ret.hash;
    }
});

module.exports = mongoose.model('SampleSizeCodeLetter', schema);

2

Answers


  1. Chosen as BEST ANSWER

    My code below successfully solve my problem:

    let aqlLevel = find.aqlLevel;
    let batchSize = find.batchSize;
    let findSampleSizeCodeLetter = await SampleSize.find({inspectionLevel: aqlLevel})
                                                       .where('lotSizeMin').lt(batchSize)
                                                       .where('lotSizeMax').gt(batchSize).exec();
    

  2. use this code both code

    let findSampleSizeCodeLetter = await SampleSize.find({
    inspectionLevel: aqlLevel,
    lotSizeMin:{$gte:batchSize},
    lotSizeMax:{$lte:batchSize}
    })
    

    or this one

    let findSampleSizeCodeLetter = await SampleSize.aggregate([
    {match:
    {
    inspectionLevel: aqlLevel,
    lotSizeMin:{$gte:batchSize},
    lotSizeMax:{$lte:batchSize}
    }
    }]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search