如何在Robo3T中查询MongoDB集合内InternalCode非空且非Null的文档?
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

