Docs Menu
Docs Home
/
MongoDB マニュアル
/ /

Null または欠落しているフィールドのクエリ

項目一覧

  • 等価フィルター
  • 非等価フィルター
  • 型チェック
  • 有無のチェック
  • MongoDB Atlas を使用した Null または欠落フィールドのクエリ

次の方法を使用して、MongoDB で null または欠落しているフィールドをクエリできます。

  • プログラミング言語のドライバー。

  • MongoDB Atlas UI。詳細については、「MongoDB Atlas を使用した Null または欠落フィールドのクエリ」を参照してください。

  • MongoDB Compass


➤ 右上の [言語選択] ドロップダウンメニューを使用して、以下の例の言語を設定するか、MongoDB Compass を選択します。


MongoDB の各種クエリ演算子は、null 値を異なる方法で処理します。

このページでは、 db.collection.find()メソッドを使用してnull値に対してmongoshで実行されるクエリ操作の例を取りあげます。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページでは MongoDB Compass を使用して、null 値をクエリする操作の例を紹介しています。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページではnull MongoCollection.Find() を使用して、 値に対して メソッドを使用した、 MongoDB C# ドライバー でのクエリ操作の例を取り上げます。 。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページでは、Collection.Find 関数を使用して、MongoDB Go Drivernull 値をクエリする操作の例を紹介しています。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページではnull com.mongodb.reactivestreams.client.MongoCollection.find メソッド を使用して、 値に対して メソッドを使用した、MongoDB Java Reactive Streams ドライバー でのクエリ操作の例を取り上げます。 。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページでは、MongoDB Java Synchronous Drivercom.mongodb.client.MongoCollection.find メソッドを使用して、null 値をクエリする操作の例を紹介しています。

Tip

ドライバーが提供する com.mongodb.client.model.Filters ヘルパー メソッドを使用すると、フィルター ドキュメントの作成が簡単になります。このページでは、これらのメソッドを使用してフィルター ドキュメントを作成する例を紹介します。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページでは、motor.motor_asyncio.AsyncIOMotorCollection.find メソッドを使用して、Motor ドライバでnull 値をクエリする操作の例を紹介しています。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページでは Collection.find() メソッドを使用して、null 値に対して MongoDB Node.js ドライバーで実行されるクエリ操作の例を取りあげます。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページではnull MongoDB PHP ライブラリ でMongoDB\\Collection::find() メソッドを使用して 値に対して実行されるクエリ操作の例を取りあげます。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページでは pymongo.collection.Collection.find メソッドを使用して、null 値に対して PyMongo Python ドライバーで実行されるクエリ操作の例を取りあげます。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページでは MongoDB ::Collection::find()メソッドを使用して、 null値に対してMongoDB Ruby ドライバー で実行されるクエリ操作の例を取りあげます。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

このページでは Collection.find() メソッドを使用して、null 値に対して メソッドを使用した、配列フィールドにおける MongoDB Scala ドライバーでのクエリ操作の例を取り上げます。

このページの例では、inventory コレクションを使用しています。MongoDB インスタンスのテスト データベースに接続し、inventory コレクションを作成します。

重要

MongoDB C# ドライバーで BsonNull.Value を使用して、MongoDB 内の null または欠落しているフィールドにクエリを実行できます。

重要

MongoDB Go ドライバーで nil を使用して、MongoDB 内の null または欠落しているフィールドに対してクエリを実行できます。

重要

MongoDBで null または欠落しているフィールドをクエリするために、Motorドライバで None を使用します。

重要

MongoDBで null または欠落しているフィールドをクエリするために、PyMongo Pythonドライバで None を使用します。

重要

MongoDB Ruby ドライバーでnilを使用して、MongoDB 内のnullまたは欠落しているフィールドをクエリします。

重要

MongoDB Scala ドライバーでBsonNull()を使用して、MongoDB 内のnullまたは欠落しているフィールドをクエリします。

db.inventory.insertMany([
{ _id: 1, item: null },
{ _id: 2 }
])
[
{ "_id": 1, "item": null },
{ "_id": 2 }
]

MongoDB Compass でドキュメントを挿入する手順については、「ドキュメントの挿入」を参照してください。

var documents = new[]
{
new BsonDocument { { "_id", 1 }, { "item", BsonNull.Value } },
new BsonDocument { { "_id", 2 } }
};
collection.InsertMany(documents);
docs := []interface{}{
bson.D{
{"_id", 1},
{"item", nil},
},
bson.D{
{"_id", 2},
},
}
result, err := coll.InsertMany(context.TODO(), docs)
Publisher<Success> insertManyPublisher = collection.insertMany(asList(
Document.parse("{'_id': 1, 'item': null}"),
Document.parse("{'_id': 2}")
));
collection.insertMany(asList(
Document.parse("{'_id': 1, 'item': null}"),
Document.parse("{'_id': 2}")
));
await db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
await db.collection('inventory').insertMany([{ _id: 1, item: null }, { _id: 2 }]);
$insertManyResult = $db->inventory->insertMany([
['_id' => 1, 'item' => null],
['_id' => 2],
]);
db.inventory.insert_many([{"_id": 1, "item": None}, {"_id": 2}])
client[:inventory].insert_many([{ _id: 1, item: nil },
{ _id: 2 }])
collection.insertMany(Seq(
Document("""{"_id": 1, "item": null}"""),
Document("""{"_id": 2}""")
)).execute()

{ item : null } クエリでは、値が null である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

{ item : null } クエリでは、値が null である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

Eq("item", BsonNull.Value)FilterDefinitionBuilder.Eq() を使用する クエリitem メソッドは、値が である フィールドを含むか、null またはitem フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

item => nil クエリでは、値が nil である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

eq("item", null) クエリでは、値が null である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

eq("item", null) クエリでは、値が null である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

{ item : None } クエリでは、値が null である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

{ item : null } クエリでは、値が null である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

[ item => undef ] クエリでは、値が null である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

{ item : None } クエリでは、値が null である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

{ item => nil } クエリでは、値が nil である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

equal("item", BsonNull) クエリでは、値が null である item フィールドを含むか、または item フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

db.inventory.find( { item: null } )

次のクエリフィルター ドキュメントをクエリ バーにコピーし、[ Findをクリックします。

{ item: null }
クエリ null 値または欠落しているフィールド
var filter = Builders<BsonDocument>.Filter.Eq("item", BsonNull.Value);
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"item", nil},
})
FindPublisher<Document> findPublisher = collection.find(eq("item", null));
FindIterable<Document> findIterable = collection.find(eq("item", null));
cursor = db.inventory.find({"item": None})
const cursor = db.collection('inventory').find({
item: null
});
$cursor = $db->inventory->find(['item' => null]);
cursor = db.inventory.find({"item": None})
client[:inventory].find(item: nil)
var findObservable = collection.find(equal("item", BsonNull()))

このクエリは、コレクション内の両方のドキュメントを返します。

存在し、かつ null 以外のフィールドをクエリするには、{ $ne : null } フィルターを使用します。{ item : { $ne : null } } クエリは、item フィールドが存在し、かつ、値が null 以外という条件に一致するドキュメントを絞り込みます。

db.inventory.find( { item: { $ne : null } } )
{ item: { $ne : null } }
var filter = Builders<BsonDocument>.Filter.Ne("item", BsonNull.Value);
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"item", bson.D{"$ne": nil}},
})
db.inventory.find( { item: { $ne : nul l} } )
collection.find($ne("item", null));
cursor = db.inventory.find( { "item": { "$ne": None } } )
const cursor = db.collection('inventory')
.find({ item: { $ne : null }
});
$cursor = $db->inventory->find(['item' => ['$ne' => null ]]);
cursor = db.inventory.find( { "item": { "$ne": None } } )
client[:inventory].find(item: { '$ne' => nil })
collection.find($ne("item", null));

{ item : { $type: 10 } } クエリが一致するのは、値が null である item フィールドを含むドキュメントのみです。つまり、item フィールドの値は BSON 型 Null(BSON 型 10)です。

{ item : { $type: 10 } } クエリが一致するのは、値が null である item フィールドを含むドキュメントのみです。つまり、item フィールドの値は BSON 型 Null(BSON 型 10)です。

FilterDefinitionBuilder.Type() メソッドを使用する Type("item", BsonType.Null) クエリは、値が null である item フィールドを含むドキュメントのみに一致します。item フィールドの値は BSON 型 Null(BSON 型 10)です。

次のクエリは、値が BSON 型 Null(BSON 型 10)である item フィールドを含むというドキュメントのみに一致します。

type("item", BsonType.NULL) クエリが一致するのは、値が null である item フィールドを含むドキュメントのみです。つまり、item フィールドの値は BSON 型 Null(BSON 型 10)です。

type("item", BsonType.NULL) クエリが一致するのは、値が null である item フィールドを含むドキュメントのみです。つまり、item フィールドの値は BSON 型 Null(BSON 型 10)です。

type("item", BsonType.NULL)クエリは、値が null である item フィールドを含むドキュメントのみと一致します。つまり、item フィールドの値は BSON 型 Null(BSON 型 10)です。

{ item : { $type: 10 } } クエリが一致するのは、値が null である item フィールドを含むドキュメントのみです。つまり、item フィールドの値は BSON 型 Null(BSON 型 10)です。

{ item : { $type: 10 } } クエリが一致するのは、値が null である item フィールドを含むドキュメントのみです。つまり、item フィールドの値は BSON 型 Null(BSON 型 10)です。

[ item => [ $type => 10 ] ] クエリが一致するのは、値が null である item フィールドを含むドキュメントのみです。つまり、item フィールドの値は BSON 型 Null(BSON 型 10)です。

{ item : { $type: 10 } } クエリが一致するのは、値が null である item フィールドを含むドキュメントのみです。つまり、item フィールドの値は BSON 型 Null(BSON 型 10)です。

{ item => { $type => 10 } } クエリが一致するのは、値が null である item フィールドを含むドキュメントのみです。つまり、item フィールドの値は BSON 型 Null(BSON 型 10)です。

db.inventory.find( { item : { $type: 10 } } )

次のクエリフィルター ドキュメントをクエリ バーにコピーし、[Find] をクリックします。

{ item : { $type: 10 } }
find null 型の検索
var filter = Builders<BsonDocument>.Filter.Type("item", BsonType.Null);
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"item", bson.D{
{"$type", 10},
}},
})
findPublisher = collection.find(type("item", BsonType.NULL));
findIterable = collection.find(type("item", BsonType.NULL));
cursor = db.inventory.find({"item": {"$type": 10}})
const cursor = db.collection('inventory').find({
item: { $type: 10 }
});
$cursor = $db->inventory->find(['item' => ['$type' => 10]]);
cursor = db.inventory.find({"item": {"$type": 10}})
client[:inventory].find(item: { '$type' => 10 })
findObservable = collection.find(bsonType("item", BsonType.NULL))

このクエリは、itemフィールドに null 値があるドキュメントのみを返します。

次の例では、フィールドを含まないドキュメントをクエリします。[1]

{ item : { $exists: false } } クエリでは、item フィールドを含まないという条件と一致するドキュメントを絞り込みます。

{ item : { $exists: false } } クエリでは、item フィールドを含まないという条件と一致するドキュメントを絞り込みます。

Exists("item", false) クエリは、FilterDefinitionBuilder.Exists() メソッドを使用して、item フィールドを含まないドキュメントに一致させます:

exists("item", false) クエリでは、item フィールドを含まないという条件と一致するドキュメントを絞り込みます。

exists("item", false) クエリでは、item フィールドを含まないという条件と一致するドキュメントを絞り込みます。

{ item : { $exists: False } } クエリでは、item フィールドを含まないという条件と一致するドキュメントを絞り込みます。

{ item : { $exists: false } } クエリでは、item フィールドを含まないという条件と一致するドキュメントを絞り込みます。

[ item => [ $exists => false ] ] クエリでは、item フィールドを含まないという条件と一致するドキュメントを絞り込みます。

{ item : { $exists: False } } クエリでは、item フィールドを含まないという条件と一致するドキュメントを絞り込みます。

{ item => { $exists => false } } クエリでは、item フィールドを含まないという条件と一致するドキュメントを絞り込みます。

exists("item", exists = false) クエリでは、item フィールドを含まないという条件と一致するドキュメントを絞り込みます。

db.inventory.find( { item : { $exists: false } } )

次のクエリフィルター ドキュメントをクエリ バーにコピーし、[Find] をクリックします。

{ item : { $exists: false } }
null 値のクエリ
var filter = Builders<BsonDocument>.Filter.Exists("item", false);
var result = collection.Find(filter).ToList();
cursor, err := coll.Find(
context.TODO(),
bson.D{
{"item", bson.D{
{"$exists", false},
}},
})
findPublisher = collection.find(exists("item", false));
findIterable = collection.find(exists("item", false));
cursor = db.inventory.find({"item": {"$exists": False}})
const cursor = db.collection('inventory').find({
item: { $exists: false }
});
$cursor = $db->inventory->find(['item' => ['$exists' => false]]);
cursor = db.inventory.find({"item": {"$exists": False}})
client[:inventory].find(item: { '$exists' => false })
findObservable = collection.find(exists("item", exists = false))

クエリは item フィールドを含んでいないドキュメントのみを返します。

Tip

以下も参照してください。

$type$exists 演算子のリファレンス ドキュメント。

[1] クエリフィルター $type: 0$exists:false の同義語として使用することはできなくなりました。null または欠落しているフィールドをクエリするには、「 Null または欠落フィールドのクエリ」を参照してください。

このセクションの例では、訓練用サンプル データセットを使用します。サンプル データセットを MongoDB Atlas 配置にロードする方法について詳しくは、「サンプル データのロード」を参照してください。

MongoDB Atlas で null または欠落しているフィールドをクエリするには、次の手順に従います。

1
  1. まだ表示されていない場合は、目的のプロジェクトを含む組織をナビゲーション バーの Organizations メニューで選択します。

  2. まだ表示されていない場合は、ナビゲーション バーのProjectsメニューからプロジェクトを選択します。

  3. Clusters ページがまだ表示されていない場合は、サイドバーの Database をクリックします。

    [ Clusters (クラスター) ] ページが表示されます。

2
  1. サンプル データを含むクラスターで、[ Browse Collections ] をクリックします。

  2. 左側のナビゲーション ペインで、sample_training データベースを選択します。

  3. companies コレクションを選択します。

3

[Insert Document] ボタンをクリックして、ダイアログ ボックスが表示されたら、[Insert] をクリックして _id フィールドのみを含むドキュメントを挿入します。

4

null または欠損値を含むドキュメントを検索するには、Filter フィールドでクエリフィルター ドキュメントを指定します。クエリフィルター ドキュメントは、クエリ演算子を使用して検索条件を指定します。

MongoDB の各種クエリ演算子は、null 値を異なる方法で処理します。クエリフィルターを適用するには、次の各ドキュメントを Filter 検索バーにコピーし、[Apply] をクリックします。

次のクエリフィルターを使用して、null 値のある description フィールドを含むか description フィールドを含まないといういずれかの条件に一致するドキュメントを絞り込みます。

{ description : null }

次のクエリフィルタを使用して、description フィールドに null 値を含むドキュメントのみを一致させます。このフィルターは、フィールドの値が BSON 型 Null(BSON 型 10)であるという必須条件を指定します。

{ description : { $type: 10 } }

次のクエリフィルターを使用して、description フィールドを含まないドキュメントのみを一致させます。以前に挿入したドキュメントのみが表示されます。

{ description : { $exists: false } }

戻る

プロジェクトの結果