Mongodb find comparing array elements

后端 未结 3 1705
说谎
说谎 2021-01-14 05:59

I have a collection with about 200K documents like this:

db.place.find()[0]
{
    \"_id\" : ObjectId(\"5290de1111afb260363aa4a1\"),
    \"name\" : \"place X\         


        
3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 06:52

    It seems that you need to use $where operator instead.

    db.place.find({$where: function() {return this.center[0] > this.center[1]}})
    

    For example, there are 3 documents in collection:

    { "_id" : ObjectId("52910457c7d99f10949e5a85"), "name" : "place X", "center" : [ 2,  3 ] }
    { "_id" : ObjectId("52910463c7d99f10949e5a86"), "name" : "place Y", "center" : [ 3,  2 ] }
    { "_id" : ObjectId("5291046ac7d99f10949e5a87"), "name" : "place Y", "center" : [ 8,  9 ] }
    

    The result of the $where command will be:

    { "_id" : ObjectId("52910463c7d99f10949e5a86"), "name" : "place Y", "center" : [ 3,  2 ] }
    

提交回复
热议问题