MongoDB高级查询 互动版

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

$mod取模运算

查询age 取模10 等于0 的数据

db.student.find( { age: { $mod : [ 10 , 0 ] } } )

举例如下:

C1 表的数据如下:

db.c1.find()
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af89afa87dc1bed94331"), "age" : 8, "length_1" : 30 }
{ "_id" : ObjectId("4fb4af8cafa87dc1bed94332"), "age" : 6, "length_1" : 30 }

查询age 取模6 等于1 的数据

> db.c1.find({age: {$mod : [ 6 , 1 ] } })
{ "_id" : ObjectId("4fb4af85afa87dc1bed94330"), "age" : 7, "length_1" : 30 }

可以看出只显示出了age 取模6 等于1 的数据,其它不符合规则的数据并没有显示出来

在users文档中查询"age"取模5 等于1的数据。

> db.users.find({age:{$mod:[5,1]}});