Mongo field A greater than field B

后端 未结 4 996
渐次进展
渐次进展 2020-12-09 15:58

i\'m trying a simple query in Mongo which in MySQL would look like this.

select * from emails where bounceCount > sentCount;

So far I ha

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-09 16:43

    db.so.find("this.bounceCount > this.sentCount") is what you are looking for.

    Equivalent: db.so.find({"$where":"this.bounceCount > this.sentCount"})

    Documentation: http://docs.mongodb.org/manual/reference/operator/where/

    Shell output:

    > db.so.insert({bounceCount:1, sentCount:2})
    > db.so.insert({bounceCount:5, sentCount:3})
    > db.so.insert({bounceCount:5, sentCount:4})
    > db.so.insert({bounceCount:5, sentCount:7})
    > db.so.insert({bounceCount:9, sentCount:7})
    
    > db.so.find()
    { "_id" : ObjectId("516d7f30675a2a8d659d7594"), "bounceCount" : 1, "sentCount" : 2 }
    { "_id" : ObjectId("516d7f37675a2a8d659d7595"), "bounceCount" : 5, "sentCount" : 3 }
    { "_id" : ObjectId("516d7f3b675a2a8d659d7596"), "bounceCount" : 5, "sentCount" : 4 }
    { "_id" : ObjectId("516d7f3d675a2a8d659d7597"), "bounceCount" : 5, "sentCount" : 7 }
    { "_id" : ObjectId("516d7f40675a2a8d659d7598"), "bounceCount" : 9, "sentCount" : 7 }
    
    > db.so.find({"bounceCount":5})
    { "_id" : ObjectId("516d7f37675a2a8d659d7595"), "bounceCount" : 5, "sentCount" : 3 }
    { "_id" : ObjectId("516d7f3b675a2a8d659d7596"), "bounceCount" : 5, "sentCount" : 4 }
    { "_id" : ObjectId("516d7f3d675a2a8d659d7597"), "bounceCount" : 5, "sentCount" : 7 }
    
    > db.so.find("this.bounceCount > this.sentCount")
    { "_id" : ObjectId("516d7f37675a2a8d659d7595"), "bounceCount" : 5, "sentCount" : 3 }
    { "_id" : ObjectId("516d7f3b675a2a8d659d7596"), "bounceCount" : 5, "sentCount" : 4 }
    { "_id" : ObjectId("516d7f40675a2a8d659d7598"), "bounceCount" : 9, "sentCount" : 7 }
    

提交回复
热议问题