mongodb如何判断字段是否存在
爱喝马黛茶的安东尼
2019-12-05 15:56:344453浏览 · 0收藏 · 0评论
$exists判断字段是否存在
查询所有存在age字段的记录
db.users.find({age: {$exists: true}});
查询所有不存在name字段的记录
db.users.find({name: {$exists: false}});
举例如下:
C1 表的数据如下:
> db.c1.find(); { "_id" : ObjectId("4fb4a773afa87dc1bed9432d"), "age" : 20, "length" : 30 } { "_id" : ObjectId("4fb4a7e1afa87dc1bed9432e"), "age_1" : 20, "length_1" : 30 }
查询存在字段age的数据:
> db.c1.find({age:{$exists:true}}); { "_id" : ObjectId("4fb4a773afa87dc1bed9432d"), "age" : 20, "length" : 30 }
可以看出只显示出了有age字段的数据,age_1的数据并没有显示出来。
查询在users文档中"sex"字段存在的记录:
db.users.find({sex: {$exists: true}});
众多python培训视频,尽在python学习网,欢迎在线学习!
关注公众号,随时随地在线学习