Last active
November 21, 2020 13:27
-
-
Save vipinrana/066e17eaba1d20ef127005db85e37dbd to your computer and use it in GitHub Desktop.
Sparse Index in mongodb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
db.collection_name.insert([ | |
{_id: 1, name: "John Doe", age: 25}, | |
{_id: 2, name: "Foo Bar", age: 35}, | |
{_id: 3, name: "Hello World"} | |
]); | |
// create sparse index | |
db.scores.createIndex( { age: 1 } , { sparse: true } ); | |
// count the sparse Indexes Field will output 2 | |
db.scores.find().hint("index_name").count(); | |
// create without sparse index | |
db.scores.createIndex( { age: 1 } ); | |
// count the non-sparse Indexes Field will output 3 | |
db.scores.find().hint("index_name").count(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment