MongoDB中的null 和not null

1、查询全部数据

> db.foo.find()
{ "_id" : ObjectId("544db3565d92133398a80daa"), "a" : 1, "name" : null}
{ "_id" : ObjectId("544db3565d92133398a80daa"), "a" : 1, "name" : "ZZZZZ" }
{ "_id" : ObjectId("5448ac1d735969c5f8386958"), "a" : 4 }
{ "_id" : ObjectId("544854ee3966c0424b50b46d"), "b" : 2 }
{ "_id" : ObjectId("544855ce735969c5f8386952"), "pwd" : "1212", "username" : "zhangsan
{ "_id" : ObjectId("544855e8735969c5f8386953"), "username" : "lisi", "pwd" : "222" }
{ "_id" : ObjectId("5448ae4c735969c5f838695d"), "username" : "tom", "age" : 23 }


2、查询name值为null

查询name为null,会查询出name字段不存的数据,如下:

> db.foo.find({name:{$in:[null]}})

{ "_id" : ObjectId("544db3565d92133398a80daa"), "a" : 1, "name" : null }
{ "_id" : ObjectId("5448ac1d735969c5f8386958"), "a" : 4 }
{ "_id" : ObjectId("544854ee3966c0424b50b46d"), "b" : 2 }
{ "_id" : ObjectId("544855ce735969c5f8386952"), "pwd" : "1212", "username" : "zhangsan

{ "_id" : ObjectId("544855e8735969c5f8386953"), "username" : "lisi", "pwd" : "222" }

查询也可用:> db.foo.find({name:null})

除第一条记录外,其它记录不存在name字段


3、name字段加上 $exists:true

> db.foo.find({name:{$in:[null],$exists:true}})
{ "_id" : ObjectId("544db3565d92133398a80daa"), "a" : 1, "name" :null}

4、查询name为不为空时(not null )

> db.foo.find({name:{$ne:null}})
{ "_id" : ObjectId("544db3b45d92133398a80dab"), "a" : 1, "name" : "zzz" }



你可能感兴趣的:(MongoDB中的null 和not null)