0

I have a Snozberry object that looks like this:

 {
  "id":"3cbdb746-561d-4e21-82e0-b7cfcad3a094",
  "thingId":"7cbfb7a6-763d-6ef1-271b-b2a0cfc2fa43",
  "stuffId":81,
  "bits":"WooHoo"
 }

Java class (constructor, getter & setters omitted for brevity):

@Document(collection="Snozberry")
public class Snozberry {
 @Id
 private UUID id;
 private UUID thingId;
 private Long stuffId;
 private String bits;
}

The repository looks like:

public interface SnozberryRepository extends MongoRepository<Snozberry,UUID>{
 Snozberry findByThingId(UUID Id);
 Snozberry findById(UUID id);
}

In the MongoDb collection, the _id & thingId fields are defined as a UUID. I would like to return the document that matches the id value I pass in.

I can return all objects using findAll() and I can return objects matching thingId by using the

  Snozberry findByThingId(UUID id)

method defined in my repository, so I know it's talking to the DB ok, and that it can find (non Id) UUID fields ok.

I've tried the default

findOne(UUID.fromString("3cbdb746-561d-4e21-82e0-b7cfcad3a094")

and i've also called this method defined in the repository

Snozberry findById(UUID id)

But both of those return null. What am I missing (feels like it's something that should be obvious).

Thanks M

MJM
  • 357
  • 1
  • 4
  • 16

1 Answers1

0

Most probably your problem lies with _id field generated by MongoDB. Since you already have an id field in you class MongoDB will try to do several things to make use of it, if not it will assign it's own _id, take a look at below page

http://docs.spring.io/spring-data/mongodb/docs/1.2.0.RELEASE/reference/html/mapping-chapter.html

Go to section 7.1.1 How the '_id' field is handled in the mapping layer

and see if it helps.

Vipin Dubey
  • 1,393
  • 3
  • 10
  • 13
  • Thanks Vipin. Veerams answer below made me take another look at the JSON returned from Spring. It's treating the Id field as a string instead of a UUID, so I suspect I need a custom converter to return it as a UUID instead. – MJM Mar 08 '17 at 05:24