예, 키의 경우 "properties.k", 값의 경우 "properties.v"와 같은 인덱싱을 통해 이를 달성할 수 있습니다. 동일한 것이sureIndex()에서 구현되는 데 사용됩니다.
먼저 예제를 보고 문서로 컬렉션을 만들어 보겠습니다. −
> db.demo274.insertOne({"details":[{StudentFirstName:"Chris",StudentLastName:"Brown"},
... {StudentFirstName:"David",StudentLastName:"Miller"},
... {StudentFirstName:"John",StudentLastName:"Smith"},
... {StudentFirstName:"John",StudentLastName:"Doe"}
...]
...}
...);
{
"acknowledged" : true,
"insertedId" : ObjectId("5e48de35dd099650a5401a42")
} find() 메서드를 사용하여 컬렉션의 모든 문서 표시 -
> db.demo274.find().pretty();
출력
이것은 다음과 같은 출력을 생성합니다 -
{
"_id" : ObjectId("5e48de35dd099650a5401a42"),
"details" : [
{
"StudentFirstName" : "Chris",
"StudentLastName" : "Brown"
},
{
"StudentFirstName" : "David",
"StudentLastName" : "Miller"
},
{
"StudentFirstName" : "John",
"StudentLastName" : "Smith"
},
{
"StudentFirstName" : "John",
"StudentLastName" : "Doe"
}
]
} 다음은 알려진 필드 이름 없이 MongoDB 하위 문서로 쿼리할 때 인덱스를 활용하는 쿼리입니다 -
> db.demo274.ensureIndex({"details.StudentFirstName": 1, "details.StudentLastName": 1}); 출력
이것은 다음과 같은 출력을 생성합니다 -
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}