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 } }