Reputation: 1195
I have created a collection and added just a name field and tried to apply the following index.
db.names.createIndex({"name":1})
Even after applying the index I see the below result. db.names.find()
{ "_id" : ObjectId("57d14139eceab001a19f7e82"), "name" : "kkkk" } { "_id" : ObjectId("57d1413feceab001a19f7e83"), "name" : "aaaa" } { "_id" : ObjectId("57d14144eceab001a19f7e84"), "name" : "zzzz" } { "_id" : ObjectId("57d14148eceab001a19f7e85"), "name" : "dddd" } { "_id" : ObjectId("57d1414ceceab001a19f7e86"), "name" : "rrrrr" }
What am I missing here.
Upvotes: 0
Views: 76
Reputation: 2650
Khans...
the way you built your index is correct however building an ascending index on names wont return the results in ascending order.
if you need results to be ordered by name you have to use
{db.names.find().sort({names:1})}
what happens when you build an index is that when you search for data the Mongo process perform the search behind the scenes in an ordered fashion for faster outcomes.
Please note: if you just want to see output in sorted order. you dont even need an index.
Upvotes: 3
Reputation: 11375
You won't be able to see if an index has been successfully created (unless there is a considerable speed performance) by running a find()
command.
Instead, use db.names.getIndexes()
to see if the index has been created (it may take some time if you're running the index in the background for it to appear in the index list)
Upvotes: 0