您好,欢迎来到化拓教育网。
搜索
您的当前位置:首页MongoDB 判断字段是否存在

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"
}

Copyright © 2019- huatuo9.cn 版权所有 赣ICP备2023008801号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务