How to create a mongo index to ensure a field is never null/absent/empty?

Only on the UNIQUE index, you can kind of mock this. You can create a document with an entry with the field / value missing and it will create the document with the NULL value in the index and WILL NOT allow further missing values.

Since your requirement is for NON UNIQUE key, I don't think that you have an option at this point in MONGODB (v2.2) to ensure that the values have to be present on the indexed column.


In mongodb, only the _id field can meet your requirement. Indexing does not seem to be a good idea (neither a feasible one) for this issue.

I think you have to enforce this rule in the insert and update operation in your app rather than asking mongodb to watch it for you.

Tags:

Mongodb