当前位置: 代码迷 >> 综合 >> mongodb下查询某个字段不为空的文档
  详细解决方案

mongodb下查询某个字段不为空的文档

热度:46   发布时间:2023-09-19 18:36:07.0

mongo客户端查询语句:

db.log_table_name.find({"bPointInfo.resInfo.result.bData.sid":"e095385bff0f44de8638b31e9215cbd0","bPointInfo.userInfo":{$ne:null}
    
}).projection({})
   .sort({_id:-1})
   .limit(100)

或者

db.log_table_name.find({"bPointInfo.resInfo.result.bData.sid":"e095385bff0f44de8638b31e9215cbd0","bPointInfo.userInfo":{"$ne":null}
    
}).projection({})
   .sort({_id:-1})
   .limit(100)

java代码中这样赋值,以下代码有公司重写的mongo的字段

listSearch.add(new SearchField("buriedPointInfo.userInfo.sid",sid));
Map<String,Object> userInfoNonNullSearch = new HashMap<>();
userInfoNonNullSearch.put("$ne",null);
listSearch.add(new SearchField("buriedPointInfo.userInfo",userInfoNonNullSearch));

1. $ne

$ne:表示not equals 就是不等于的意思

# 查询某字段不为空的数据
db.hfijf.find({fieldName: {$ne:null}})
# 查询字段等于空的数据
db.hfijf.find({fieldName: {$eq:null}})

2. $exists

$exists:表示是否存在。值为false表示不存在,值为true表示存在

# 查询某字段不为空的数据
db.fdafdsa.find({fieldName:{$exists:true}})
# /查询某字段不存在的数据
db.fdafdsa.find({fieldName:{$exists:false}})
 

  相关解决方案