I have 4 fields in my MongoDB
database date,source,uid,time
I want to create index on date but i also want to ensure that no two rows have the same value of date,source,uid,time
..one way of doing this is this is (as I read in documentation)
db.things.ensureIndex({date : 1, source:1, uid:1, time:1},开发者_如何学JAVA {unique : true, dropDups : true})
but it is using compound keys index..i only want the index on date and i also want to ensure that no two tows have the same value for date,source,uid and time.
please help me how to do it?
To ensure uniqueness you need an index on (date, source, uid, time), and since date is the first property in this index Mongo can use it for queries on just date too. In fact, the index can be used on queries involving (date, source), (date, source, uid) and (date, source, uid, time), too.
In other words, an index can be used on any subset of properties that occurs in the index, provided they form a prefix of the index.
精彩评论