且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何基于Mongodb的doc字段中的每个数组项过滤集合

更新时间:2023-08-27 12:25:10

对于示例,您可以使用点符号并运行以下查询:

You can use the dot notation and run following query for your example:

{"parts.0":{"$gte":1},"parts.1":{"$gte":5}}

蒙戈游乐场

或使用下面的JS代码构建更通用的内容:

or use below JS code to build something more generic:

let input = [1,5];
let query = Object.fromEntries(input.map((val, i) => ([ "parts." + i, { $gte: val } ])));

console.log(query);