Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to index my collection to use a compound multikey index?
For this, use ensureIndex(). Let us create a collection with documents −
> 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")
}
Display all documents from a collection with the help of find() method −
> db.demo678.find();
This will produce the following output −
{ "_id" : ObjectId("5ea4276904263e90dac943fc"), "id" : 101, "details" : [
{ "userId" : "1001", "userName" : "Chris" },
{ "userId" : "1002", "userName" : "David" }
], "otherDetails" : [
{ "CountryName" : "US", "EmailId" : [ "Chris@gmail.com", "David@gmail.com" ] }
] }Advertisements