इसके लिए, ensureIndex() का उपयोग करें। आइए दस्तावेज़ों के साथ एक संग्रह बनाएं:
> db.demo678.ensureIndex({id:1,"details.userId":1});
{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 }
> db.demo678.insertOne(
{
id:101,
"details" : [
{
"userId" : "1001",
"userName":"Chris"
},
{
"userId" : "1002",
"userName":"David"
}
],
"otherDetails" : [
{
CountryName:"US",
EmailId:["Chris@gmail.com","David@gmail.com"]
}
]
});
{ "acknowledged" : true, "insertedId" : ObjectId("5ea4276904263e90dac943fc") }
find() मेथड की मदद से संग्रह के सभी दस्तावेज़ प्रदर्शित करें:
> db.demo678.find();
यह निम्न आउटपुट देगा:
{ "_id" : ObjectId("5ea4276904263e90dac943fc"), "id" : 101, "details" : [ { "userId" : "1001", "userName" : "Chris" }, { "userId" : "1002", "userName" : "David" } ], "otherDetails" : [ { "CountryName" : "US", "EmailId" : [ "Chris@gmail.com", "David@gmail.com" ] } ] }