MongoDB 查询 (两个字段差值作为条件查询)
为情所困
为情所困 2017-04-27 09:02:02
0
2
682

字段

  • name string
  • x int
  • y int

表记录

name x y
jhon 1 2
lily 2 1
gan 3 2

查询结果

查询所有 x > y 的所有 name

为情所困
为情所困

reply all(2)
给我你的怀抱
db.collections.find({'$where': "this.x > this.y"}, {'name': 1})
迷茫

This way, you can also sort by the difference between x and y

db.collection.aggregate(
    [  
      {
        $project : {
           _id: '$name',
           val: { $subtract : [ "$x", "$y" ] },
           x: '$x',
           y: '$y'
        }
      },
      {$match: {val: {$gt: 0}}},
      {$sort: { val: -1 }}
   ]
)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!