MongoDBのフィールドが[]または{}かどうかを確認するにはどうすればよいですか?
MongoDBのフィールドが[]または{}であるかどうかを確認するには、次の構文を使用できます-
db.yourCollectionName.find({
"yourOuterFieldName": { "$gt": {} },
"yourOuterFieldName.0": { "$exists": false }
}); まず、ドキュメントを使用してコレクションを作成しましょう-
> db.checkFieldDemo.insert([
... { _id: 1010, StudentDetails: {} },
... { _id: 1011, StudentDetails: [ { StudentId: 1 } ] },
... { _id: 1012, StudentDetails: [ {} ] },
... { _id: 1013 },
... { _id: 1014, StudentDetails: null},
... { _id: 1015, StudentDetails: { StudentId: 1 } }
... ]);
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 6,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
}) 以下は、find()メソッドを使用してコレクションからすべてのドキュメントを表示するためのクエリです-
> db.checkFieldDemo.find().pretty();
これにより、次の出力が生成されます-
{ "_id" : 1010, "StudentDetails" : { } }
{ "_id" : 1011, "StudentDetails" : [ { "StudentId" : 1 } ] }
{ "_id" : 1012, "StudentDetails" : [ { } ] }
{ "_id" : 1013 }
{ "_id" : 1014, "StudentDetails" : null }
{ "_id" : 1015, "StudentDetails" : { "StudentId" : 1 } } 以下は、MongoDBのフィールドが[]または{}-
であるかどうかを確認するためのクエリです。> db.checkFieldDemo.find({
... "StudentDetails": { "$gt": {} },
... "StudentDetails.0": { "$exists": false }
... }); これにより、次の出力が生成されます-
{ "_id" : 1015, "StudentDetails" : { "StudentId" : 1 } } -
MongoDBコレクションの空のフィールドをチェックする方法は?
MongoDBコレクションの空のフィールドを確認するには、$existsと$eq演算子を併用します。ドキュメントを使用してコレクションを作成しましょう- > db.demo485.insertOne({"FirstName":"Chris","LastName":""});{ "acknowledged" : true, "insertedId" : ObjectId("5e82e9f6b0f3fa88e22
-
MongoDBのidフィールドを非表示
ドキュメントを使用してコレクションを作成しましょう- > db.demo575.insertOne({id:101,Information:{Name:"Chris",Age:21}});{ "acknowledged" : true, "insertedId" : ObjectId("5e916a55581e9acd78b427f7") } > db.demo575.insertOne({id:102,Information:{Name:"David",A