MongoDB query: match whether a field is null or exists

1. Usage

db.collectionNamedb.getCollection('collectionName')Both means to specify a certain Collection.

1.1 Match whether a field exists

// 仅查询某个字段查询存在的记录
db.collectionName.find({
    
    "field name":{
    
    $exists:true}});

// 仅查询某个字段查询不存在的记录
db.collectionName.find({
    
    "field name":{
    
    $exists:false}});

1.2 Match a field as null

// 仅查询某个字段为空的记录
// 既然为 null 那么就表示该字段一定存在
db.collectionName.find({
    
    "field name":{
    
    $exists: true, $eq: null}})
  • Need to pay attention 1.4 过滤某个字段为 null 或不存在to the difference with ;
  • MongoIn , nullthe data for must exist;

1.3 Match a field that is not null and exists

// 仅查询某个字段为不为空且存在的记录
db.collectionName.find({
    
    "field name":{
    
    $ne:null}});

【Note】In fact, it is 1.4 过滤某个字段为 null 或不存在the inversion of .

1.4 Match a field is null or does not exist

It will match both the field 不存在and the data whose field is .null

db.collectionName.find({
    
    "field name":null});

// 与上面语句一样的效果
db.collectionName.find({
    
    "field name":{
    
    $eq:null}});

2. Examples

Insert test data:

db.getCollection("null_demo").insert( {
    
    
    name: "张三",
    gender: 0,
    address: "上海市"
} );

db.getCollection("null_demo").insert( {
    
    
    name: "李四",
    gender: 0 
    // 没有 address 字段
} );

db.getCollection("null_demo").insert( {
    
    
    name: "王五",
    gender: 0,
    address: null // address 字段为 null
} );

Query the inserted test data:

db.null_demo.find()

search result:

_id name gender address
64681d503902000019003aa2 Zhang San 0 Shanghai
64681e003902000019003aa3 Li Si 0 (N/A)
64681e1d3902000019003aa4 Wang Wu 0 (Null)

【Note】N/A: Not applicable ( Not Applicable).

2.1 Match whether the address field exists

// 仅查询某个字段查询存在的记录
db.null_demo.find({
    
    "address":{
    
    $exists:true}});

The output is as follows:

_id name gender address
64681d503902000019003aa2 Zhang San 0 Shanghai
64681e1d3902000019003aa4 Wang Wu 0 (Null)
// 仅查询某个字段查询不存在的记录
db.null_demo.find({
    
    "address":{
    
    $exists:false}});

The output is as follows:

_id name gender
64681e003902000019003aa3 Li Si 0

2.2 Match address field is null

// 仅查询某个字段为空的记录
db.null_demo.find({
    
    "address":{
    
    $exists: true, $eq: null}})

The output is as follows:

_id name gender address
64681e1d3902000019003aa4 Wang Wu 0 (Null)

2.3 The matching address field is not null and exists

// 仅查询某个字段为不为空且存在的记录
db.null_demo.find({
    
    "address":{
    
    $ne:null}});

The output is as follows:

_id name gender address
64681d503902000019003aa2 Zhang San 0 Shanghai

2.4 Matching address is null or does not exist

db.null_demo.find({
    
    "address":null});

// 与上面语句一样的效果
db.null_demo.find({
    
    "address":{
    
    $eq:null}});

The output is as follows:

_id name gender address
64681e003902000019003aa3 Li Si 0 (N/A)
64681e1d3902000019003aa4 Wang Wu 0 (Null)

End!

Personal blog: Roc's Blog

Guess you like

Origin blog.csdn.net/peng2hui1314/article/details/130593503