您现在的位置是:课程教程文章

mongodb如何判断字段是否存在

2023-12-18 21:07课程教程文章 人已围观

$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学习网,欢迎在线学习!

课程教程:mongodb如何判断字段是否存在

上一篇:powershell打不开python怎么解决

下一篇:没有了

站点信息

  • 文章统计篇文章