MongoDB queries with null value

前端 未结 2 845
不思量自难忘°
不思量自难忘° 2020-12-11 01:16

My collection (MongoDB v 2.0.2) has following records:

db.organization.find({})
{ \"_id\" : 1001, \"path\" : [ ], \"parent\" : null }
{ \"_id\" : 1002, \"pat         


        
2条回答
  •  一整个雨季
    2020-12-11 01:23

    Just checked following script at 2.0 and 2.0.2:

    db.items.insert({ "_id" : 1001, "path" : [ ], "parent" : null })
    db.items.insert({ "_id" : 1002, "path" : [ 1001 ], "parent" : NumberLong(1001) })
    db.items.ensureIndex({"path":1});
    db.items.ensureIndex({"parent":1},{sparse:false});
    db.items.find({"parent":null})
    

    actually returns one document that you expect:

    { "_id" : 1001,
      "path" : [],
      "parent" : null } 
    

    Also you can look into this doc about querying and nulls, probably should help you avoid possible future mistakes.

提交回复
热议问题