温馨提示×

mongodb如何判断数据是否存在

小晨
1485
2021-07-08 10:45:21
栏目: 云计算

mongodb判断数据是否存在的方法:1、打开终端命令行;2、启动mongodb服务;3、打开mongodb可视化管理工具,编写shell脚本;4、在脚本中查询数据时使用$exists方法判断某字段数据是否存在即可。

mongodb如何判断数据是否存在

具体内容如下:

使用 $exists 判断字段是否存在

###### 所有数据

> db.book.find({})

{

    "_id" : ObjectId("58b395fbab449b190054c556"),

    "title" : "MongoDB 教程",

    "description" : "MongoDB 是一个 Nosql 数据库",

    "owner" : "Knight"

}

/* 2 */

{

    "_id" : ObjectId("58b3960dab449b190054c557"),

    "title" : "MongoDB 教程",

    "description" : "MongoDB 是一个 Nosql 数据库",

    "owner" : "Knight",

    "date" : "2017-02-27"

}

######查询所有存在 date 字段的记录

> db.book.find({date:{$exists:true}})

/* 1 */

{

    "_id" : ObjectId("58b3960dab449b190054c557"),

    "title" : "MongoDB 教程",

    "description" : "MongoDB 是一个 Nosql 数据库",

    "owner" : "Knight",

    "date" : "2017-02-27"

}

######查询所有不存在 date 字段的记录

> db.book.find({date:{$exists:false}})

/* 1 */

{

    "_id" : ObjectId("58b395fbab449b190054c556"),

    "title" : "MongoDB 教程",

    "description" : "MongoDB 是一个 Nosql 数据库",

    "owner" : "Knight"

}

0