Mongodb find comparing array elements

后端 未结 3 1706
说谎
说谎 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:51

    You can not do the query you want in a simple way in mongo because mongo does not support searching or updating based on the element in the collection. So even such simple document as {a : 1, b : 1} and find the document where a = b is impossible without $where clause.

    The solution suggested by idbentley db.place.find({'center.0':{'$gt':'center.1'}}) will not work as well (also will not generate an error) because this way you will compare center.0 to a string 'center.1'. Therefore correct solution is the solution of Victoria Malaya (but she forgot to put .count() in the end).

    One thing I would like to suggest. Anything with where is very very slow. So if you plant to do this query more then once, think about creating additional field which will store this precomputed result (you can do it in a similar fashion with this answer).

提交回复
热议问题