Mongodb Query To select records having a given key

前端 未结 3 1202
[愿得一人]
[愿得一人] 2020-11-28 07:09

The records in my database are

{\"_id\":\"1\",\"fn\":\"sagar\",\"ln\":\"Varpe\"}

{\"_id\":\"1\",\"fn\":\"sag\",\"score\":\"10\"}

{\"_id\":\"1\",\"ln\":\"ln         


        
相关标签:
3条回答
  • 2020-11-28 07:15

    I had the same problem and

    db.coll.find({"mykey":{'$exists': 1}})
    

    worked for me

    0 讨论(0)
  • 2020-11-28 07:28

    To find if a key/field exists in your document use the $exists operator.

    Via the MongoDB shell ...

    db.things.find( { ln : { $exists : true } } );
    
    0 讨论(0)
  • 2020-11-28 07:31
    db.collection.find({ ln: { $exists: true} });
    

    The $size operator matches any array with the number of elements specified by the argument. For example:

    db.collection.find({ ln: { $exists: true, $size: 0 } });
    

    $size does not accept ranges of values. To select documents based on fields with different numbers of elements, create a counter field that you increment when you add elements to a field.

    0 讨论(0)
提交回复
热议问题