You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mongoose语法求助:如何查询batchSize介于lotSizeMin与lotSizeMax间的数据

正确的Mongoose查询语法

问题原因

你当前的查询写法有误:.where(batchSize)是把变量batchSize的值当成了集合的字段名,而不是用这个值去匹配集合里lotSizeMin和lotSizeMax的范围,所以查不到数据。正确逻辑是找inspectionLevel等于aqlLevel,且batchSize落在lotSizeMin到lotSizeMax区间内的文档。

两种可行的查询写法

写法一:链式调用方式

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

写法二:直接在查询条件中定义

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

逻辑说明

两种写法效果完全一致:

  1. 先筛选出inspectionLevel与传入的aqlLevel匹配的文档
  2. 再进一步过滤出批次大小区间包含batchSize的记录(即lotSizeMin ≤ batchSize ≤ lotSizeMax)

内容的提问来源于stack exchange,提问作者Freedom Trail

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 22:37:29