Mark Serrano opened DATAMONGO-44 and commented
When creating a simple POJO with an id property and persisting this object to MongoDB, the id property is overridden by Spring Data. It merges the "id" with "_id".
For example:
{ "_id" : "69133244-fcc0-42b2-aa59-308f00c75860", "firstName" : "John", "money" : 1000, "lastName" : "Smith" }
{ "_id" : "45d236ff-6a38-4767-a973-6f3d70ff2448", "firstName" : "Jane", "money" : 2000, "lastName" : "Adams" }
{ "_id" : "9dcba269-9ed5-45c6-887e-a2de8fec334d", "firstName" : "Jeff", "money" : 3000, "lastName" : "Mayer" }
The native MongoDB preserves both the "id" and "_id", while Spring Data combines both as "_id". As a workaround, I have to rename my POJO's id property as "pid" (any other name will work).
For example:
{ "_id" : ObjectId("4d5e7b3bfdece02281f253a4"), "id" : "f1e6b8b2-9d68-495c-9d6e-b81cc35cf9bc", "firstName" : "John", "lastName" : "Smith", "money" : 1000 }
{ "_id" : ObjectId("4d5e7b3bfdece02282f253a4"), "id" : "39f6ebfb-10cf-4702-a5b8-8bc2489cebcc", "firstName" : "Jane", "lastName" : "Adams", "money" : 2000 }
{ "_id" : ObjectId("4d5e7b3bfdece02283f253a4"), "id" : "f4932004-c320-47e4-b4b3-7d07f3aaabb9", "firstName" : "Jeff", "lastName" : "Mayer", "money" : 3000
See the following tutorial that shows this "pid" workaround: http://krams915.blogspot.com/2011/02/spring-data-mongodb-tutorial.html
See the following tutorial that uses native MongoDB which correctly preserves both "id" and "_id": http://krams915.blogspot.com/2011/01/spring-mvc-3-using-document-oriented.html
Reference URL: http://forum.springsource.org/showthread.php?p=346693&posted=1#post346693
Issue Links: