Hi John,
thanks for pointing me in the right direction.
My app was actually running Mongo in safe mode already, so I've done some testing with the safe example in MongoMapper. When I run the safe.rb file, it works as expected. However when I modify it slightly into this
https://gist.github.com/2157677 then the behaviour is somewhat unexpected. I added two keys, and the :_id always has the same value. I also added a :unique index on :_id.
When I run the file, it keeps overwriting the record, rather than raising an exception for duplicate IDs — even when using User.create. My guess is, it has to do with Brian's observation that in MongoMapper, the _id is set before it hits the Ruby driver. Then likely the Ruby driver interprets it as an update to an existing record, rather than a create. I can also see that in the log files, where the Ruby driver performs update instead of insert, even for new documents. When I remove the manual :_id key from my models and let it use ObjectIds, it's still the same.
I'm not sure if this is a bug or expected behaviour, but for now it seems the result for using custom IDs with MM is, you always have to check manually whether a record with that ID already exists, to prevent existing ones from being overwritten.
Any ideas?