You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Robo3T中查询MongoDB集合内InternalCode非空且非Null的文档?

Solution for Querying Non-Null/Non-Empty InternalCode in MongoDB (Robo3T)

Got it, let's break down what's going on here and fix your query.

Your current statement:

db.getCollection('db1.customer').find({ InternalCode : { $exists: true, $ne: null } })

works for filtering documents where InternalCode exists and isn't null, but it misses another common "empty" case: empty strings ("") (and potentially other empty values like empty arrays if those are present in your data).

Fix 1: Exclude Null and Empty Strings

If your definition of "non-null/non-empty" means the field exists, isn't null, and isn't an empty string, update your query to add a $ne: "" condition:

// Make sure you're in the db1 database first (run `use db1` if not)
db.getCollection('customer').find({
  InternalCode: {
    $exists: true,
    $ne: null,
    $ne: ""
  }
})

This will return only documents where InternalCode is present, has a value that's not null, and isn't an empty string.

Fix 2: Exclude All "Blank" Values (Including Whitespace-Only Strings)

If you also want to exclude strings that are just whitespace (like " "), use a regular expression to match at least one non-whitespace character:

db.getCollection('customer').find({
  InternalCode: {
    $exists: true,
    $ne: null,
    $regex: /\S/
  }
})

The \S regex matches any character that's not a whitespace, so this ensures the field has actual content.

Quick Note About Robo3T

Double-check that you've selected the db1 database in Robo3T's sidebar before running the query. Alternatively, you can explicitly target the database like this:

db1.customer.find({
  InternalCode: {
    $exists: true,
    $ne: null,
    $ne: ""
  }
})

内容的提问来源于stack exchange,提问作者user1765862

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 17:27:54