Mongoose语法:如何查找参数在两个字段之间的数据。

huangapple go评论55阅读模式
英文:

Mongoose syntax : how to find data where parameter in between two fields

问题

我想使用Mongoose在MongoDB Atlas中查找数据记录。aqlLevel和batchSize是已知的值,但inspectionLevel、lotSizeMin和lotSizeMax是MongoDB Atlas中表的字段。我尝试过没有where条件的语法,它可以正常工作。然而,使用where条件时,我无法从MongoDB Atlas的表中获取任何数据。是否有人可以帮助我使用Mongoose语法来带有where条件查找数据?

我想要使用以下代码来搜索:

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

表的架构如下所示:

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);

请注意,上述代码是在使用Mongoose进行MongoDB查询时的示例代码。

英文:

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);

答案1

得分: 0

使用这段代码:

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

或者这段代码:

let findSampleSizeCodeLetter = await SampleSize.aggregate([
  {
    $match: {
      inspectionLevel: aqlLevel,
      lotSizeMin: { $gte: batchSize },
      lotSizeMax: { $lte: batchSize }
    }
  }
])
英文:

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}
}
}]);

答案2

得分: 0

我的以下代码成功解决了我的问题:

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

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();

huangapple
  • 本文由 发表于 2023年7月10日 11:04:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76650452.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定