title: Preventing duplicates in MongoDB using Spring Data (Spring Roo)
body: <p>I have been trying to get my head wrapped around MongoDB, as it's used by Spring, so I decided to start a little project in Spring Roo.</p>
  
  <p>In my project, I am storing my User Login data to MongoDB.  The trouble is that the registration process, which creates a new User object and stores it in the MongoDB, has a tendency to create duplicates despite the fact I have @Unique on the loginId field.</p>
  
  <p>Now, I know part of the problem is that I am thinking about things from a JPA/RDBMS perspective, and MongoDB is not a relational DB and thus has a different set of parameters in which to operate with, but I having trouble finding guidance in anything more than a VERY simple sample code.</p>
  
  <p>First, what Spring/Other annotations are available, and more importantly, commonly used when dealing with MongoDB from a Spring-world?  Second, when dealing with documents that need to be "uniqued", how does one typically do this?  Do you first search on the unique field to ensure it's not already there first, then do the insert?  Third, in JPA-land, I could use the annotations @PrePersist and @PreUpdate to do last-minute data manipulation, like MD5-hashing passwords that have been updated or adding/updating a "Last Modified" date just prior to storing.  I know this are JPA-isms, but can I still use those, and if not, is there an alternative for use with Spring Data/MongoDB?</p>