MongoDB查询是否为空

1、为null或者不存在l

db.test.find({"test":null});

2、不为null并且存在记录

db.test.find({"test":{"$ne":null}});
db.test.find({"test":{"$ne":null, $exists:true}});

3、存在

db.test.find({"test":{$exists:true}});

4、不存在(不会返回null的值)

db.test.find({"test":{$exists:false}});

5、存在且不为null,不为""(空字符串)

List testList = new ArrayList<>();

testList .add(null);

testList .add("");

queryUser.put("test", new BasicDBObject("$nin", testList));

 

  • 5
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
查询MongoDB中字段不为空的数据,可以使用以下查询语句: 引用\[1\]: db.hfijf.find({fieldName: {$ne:null}}) 这条语句会返回fieldName字段不为空的数据。 另外,如果要查询字段存在且不为空的数据,可以使用以下查询语句: 引用\[2\]: db.fdafdsa.find({fieldName:{$exists:true}}) 这条语句会返回存在fieldName字段且不为空的数据。 需要注意的是,如果涉及到数组字段,使用$size操作符的查询不会使用索引。可以使用以下查询语句来查询数组字段不为空的数据: 引用\[3\]: ME.find({ pictures: { $exists: true, $ne: \[\] } }) 这条语句会返回pictures字段存在且不为空的数据。 #### 引用[.reference_title] - *1* *2* [mongodb查询某个字段不为空的文档](https://blog.csdn.net/qq_34412985/article/details/121364177)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [查找数组字段不为空的MongoDB记录](https://blog.csdn.net/xfxf996/article/details/106017388)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值