网站地图    收藏   

主页 > 后端 > MongoDB >

MongoDB中的加减乘除运算详解

来源:自学PHP网    时间:2019-08-01 18:22 作者:小飞侠 阅读:

[导读] MongoDB中的加减乘除运算详解...

前言

1.$add操作符(+)

$add操作符的语法:

{ "_id" : 1, "item" : "abc", "price" : 10, "fee" : 2, date: ISODate("2014-03-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "fee" : 1, date: ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "fee" : 0, date: ISODate("2014-03-15T09:00:00Z") }
db.sales.aggregate(
 [
 { $project: { item: 1, total: { $add: [ "$price", "$fee" ] } } }
 ]
)
{ "_id" : 2, "item" : "jkl", "total" : 21 }
{ "_id" : 3, "item" : "xyz", "total" : 5 }

有效日期 命名为 expire_date,则相应的SQL如下:

db.sales.aggregate(
 [
 { $project: { item: 1, expire_date: { $add: [ "$date", 3*24*60*60*1000 ] } } }
 ]
)
{ "_id" : 2, "item" : "jkl", "expire_date" : ISODate("2014-03-04T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "expire_date" : ISODate("2014-03-18T09:00:00Z") }
注:此案例中有关时间的这种运算,在SQL Server 数据库中是通过函数 DATEADD()来实现的。2.$subtract操作符(-)

$subtract操作符的语法

{ "_id" : 1, "item" : "abc", "price" : 10, "fee" : 2, "discount" : 5, "date" : ISODate("2014-03-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "fee" : 1, "discount" : 2, "date" : ISODate("2014-03-01T09:00:00Z") }

db.sales.aggregate( [ { $project: { item: 1, total: { $subtract: [ { $add: [ "$price", "$fee" ] }, "$discount" ] } } } ] )
{ "_id" : 2, "item" : "jkl", "total" : 19 }
db.sales.aggregate( [ { $project: { item: 1, dateDifference: { $subtract: [ new Date(), "$date" ] } } } ] )
{ "_id" : 2, "item" : "jkl", "dateDifference" : NumberLong("154743494624") }
注:此案例中关于时间的这种运算,在SQL Server 数据库中是通过函数 DATEDiff()来实现的。

实现SQL的语句如下

db.sales.aggregate( [ { $project: { item: 1, dateDifference: { $subtract: [ "$date", 3 * 60 * 60 * 1000 ] } } } ] )
{ "_id" : 2, "item" : "jkl", "dateDifference" : ISODate("2014-03-01T06:00:00Z") }

3.$multiply操作符(*)

{ "_id" : 1, "item" : "abc", "price" : 10, "quantity": 2, date: ISODate("2014-03-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity": 1, date: ISODate("2014-03-01T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity": 10, date: ISODate("2014-03-15T09:00:00Z") }

 db.sales.aggregate(
 [
 { $project: { item: 1, total_price: { $multiply: [ "$price", "$quantity" ] } } }
 ]
)
{ "_id" : 2, "item" : "jkl", "total_price" : 20 }
{ "_id" : 3, "item" : "xyz", "total_price" : 50 }
4.$divide操作符(/)

{ "_id" : 1, "name" : "A", "hours" : 80, "resources" : 7 },
{ "_id" : 2, "name" : "B", "hours" : 40, "resources" : 4 }

db.planning.aggregate(
 [
 { $project: { name: 1, workdays: { $divide: [ "$hours", 8 ] } } }
 ]
)
{ "_id" : 2, "name" : "B", "workdays" : 5 }
5 求余 运算($mod)
{ "_id" : 2, "project" : "B", "hours" : 40, "tasks" : 4 }
db.planning.aggregate(
 [
 { $project: { remainder: { $mod: [ "$hours", "$tasks" ] } } }
 ]
)
{ "_id" : 2, "remainder" : 0 }
6.案例分析
{ "_id" : 1, "sno" : 201501010001, "name" : "xiaoming", "attendanceAvg" : 90, "quizzAvg" : 95, "testAvg" : 98, "teacherPet" : true }
{ "_id" : 2, "sno" : 201501010002, "name" : "xiaoli", "attendanceAvg" : 100, "quizzAvg" : 90, "testAvg" : 90, "teacherPet" : false }
{ "_id" : 3, "sno" : 201501010003, "name" : "xiaohong", "attendanceAvg" : 100, "quizzAvg" : 80, "testAvg" : 100, "teacherPet" : false }
db.students.aggregate(
 [
 {
  $project:
  {

   "sno": 1, "name": 1,
   grade:
   {
    $cond: ["$teacherPet", 100, {
    "$add":
     [
     { "$multiply": ["$attendanceAvg", .1] },
     { "$multiply": ["$quizzAvg", .3] },
     { "$multiply": ["$testAvg", .6] }
     ]
    }]
   }
  }
 }
 ]
)
{ "_id" : 2, "sno" : 201501010002, "name" : "xiaoli", "grade" : 91 }
{ "_id" : 3, "sno" : 201501010003, "name" : "xiaohong", "grade" : 94 }

6.2 常用逻辑表达式

类别布尔表达式 {$and: [ , , ... ]} 控制表达式 或者
{ $cond: [ , , ] }
犎绻鹐xpression是null,那就返回replacement-expression-if-null,否则返回expression。比较表达式 爗?strcasecmp:燵,燷爙

通过这些操作符,就可以在聚合中使用更复杂的逻辑。总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对自学php网的支持。

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论