I'm working on porting my tutorial
Spring MVC 3: Using a Document-Oriented Database - MongoDB to use Spring Data Document - MongoDB Support 1.0.0.M1. However I think I've found a bug.
The bug happens when your document (or your domain class) has an "id" property.
Using plain MongoDB (no Spring Data support), to populate the Mongo with sample data, I call the init() method:Code:public class Person implements Serializable { private String id; private String firstName; private String lastName; private Double money; ... }
Calling db.mycollection.find() on the Mongo console:Code:private void init() { // Populate our MongoDB database logger.debug("Init MongoDB users"); // Drop existing collection MongoDBFactory.getCollection("mydb","mycollection").drop(); // Retrieve collection. If not existing, create a new one DBCollection coll = MongoDBFactory.getCollection("mydb","mycollection"); // Create new object BasicDBObject doc = new BasicDBObject(); // Generate random id using UUID type 4 // See http://en.wikipedia.org/wiki/Universally_unique_identifier doc.put("id", UUID.randomUUID().toString()); doc.put("firstName", "John"); doc.put("lastName", "Smith"); doc.put("money", 1000); coll.insert(doc); // Create new object doc = new BasicDBObject(); // Generate random id using UUID type 4 doc.put("id", UUID.randomUUID().toString()); doc.put("firstName", "Jane"); doc.put("lastName", "Adams"); doc.put("money", 2000); coll.insert(doc); // Create new object doc = new BasicDBObject(); // Generate random id using UUID type 4 doc.put("id", UUID.randomUUID().toString()); doc.put("firstName", "Jeff"); doc.put("lastName", "Mayer"); doc.put("money", 3000); coll.insert(doc); }
Notice that all properties, id, firstName, lastName, and money are present. We also have another property "_id". This works fine.Code:{ "_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 }
Now, I ported the code to use Spring Data MongoDb. To populate Mongo with data, I call the following init() method:
Notice I used the template:Code:private void init() { // Populate our MongoDB database logger.debug("Init MongoDB users"); // Drop existing collection mongoTemplate.dropCollection("mycollection"); // Create new object Person p = new Person (); p.setId(UUID.randomUUID().toString()); p.setFirstName("John"); p.setLastName("Smith"); p.setMoney(1000.0); // Insert to db mongoTemplate.insert("mycollection", p); // Create new object p = new Person (); p.setId(UUID.randomUUID().toString()); p.setFirstName("Jane"); p.setLastName("Adams"); p.setMoney(2000.0); // Insert to db mongoTemplate.insert("mycollection", p); // Create new object p = new Person (); p.setId(UUID.randomUUID().toString()); p.setFirstName("Jeff"); p.setLastName("Mayer"); p.setMoney(3000.0); // Insert to db mongoTemplate.insert("mycollection", p); }
Calling db.mycollection.find() on the Mongo console:Code:mongoTemplate.insert("mycollection", p);
Notice the "id" property is gone. Actually it has been merged with the property "_id". In the original implementation, these are two separate properties.Code:{ "_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 real problem starts when you try to update and remove the item by "id". But the fundamental question is why the discrepancy between the two implementations?
As a workaround, I had to name the property id to something different like "pid" and everything works as expected with Spring Data MongoDb.


Reply With Quote
I've checked the Spring Data for MongoDB source and you're right. Somehow the id is hard coded. I wouldn't call it bug or wrong behavior if the native MongoDB behaves exactly the same, but it behaves differently.
