views:

124

answers:

1
+2  Q: 

MongoDB indexes

I'm in the process of converting my Rails app to use mongodb through mongoid. I have two questions relating to indexes. I think I know the answer, but I want confirmation from someone who has more experience with mongodb.

Let's look at the following example where I have one relational association between Users and Posts.

user.rb

class User  
    has_many_related :posts  
end

post.rb

class Post  
    belongs_to_related :user  
end

Now when I look at the indexes created through the MongoHQ interface, I notice the following two:

  1. Key Name: _id_
    Indexed Field: _id
    Unique: <blank>
    Is the id guaranteed to be unique? If so, why isn't unique set. If not, how can I set this and do I need to?

  2. Key Name: user_id_1
    Indexed Field: user_id
    Unique: false
    Am I correct in assuming the Indexed Field is the field name in the collection? Just want to confirm as Key Name has the _1 after it.

+2  A: 

Yes, _id in MongoDB is always unique. It's the primary key, which is why setting UNIQUE isn't necessary.

ceejayoz