Querying null value in MongoDB

后端 未结 2 965
执念已碎
执念已碎 2021-01-03 10:45

Using pymongo I am trying to retrieve the documents in a collection that have a SmallUrl different from null. I\'m trying to get t

相关标签:
2条回答
  • 2021-01-03 11:03

    The pymongo version of null is the Python None. So query should look like:

    query = {
        'Name': {'$regex': search_title, '$options': 'i'}, 
        'Images.SmallUrl': {'$ne': None}}
    
    0 讨论(0)
  • 2021-01-03 11:23

    your query does not work because you should use 'Images.SmallUrl' instead of 'SmallUrl' for the key of query. my test collection:

    > db.t.find()
    { "_id" : ObjectId("512cdbb365fa12a0db9d8c35"), "Images" : { "LargeUrl" : "http://aaa.com", "SmallUrl" : "http://bb.com" }, "Name" : "yy" }
    { "_id" : ObjectId("512cdc1765fa12a0db9d8c36"), "Images" : { "LargeUrl" : "http://aaa.com", "SmallUrl" : null }, "Name" : "yy" }
    

    and my test query:

    > db.t.find({'Images.SmallUrl': {$ne: null}})
    { "_id" : ObjectId("512cdbb365fa12a0db9d8c35"), "Images" : { "LargeUrl" : "http://aaa.com", "SmallUrl" : "http://bb.com" }, "Name" : "yy" }
    

    Hope to help ^_^

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