-
Aug 13th, 2011, 09:42 AM
#1
MongoDB @Query with ObjectId runtime parameter
@Document(collection="users")
class User {
@Id
private ObjectId id;
private ObjectId pictureId;
}
The following query on a users collection repository is not working
@Query(value= "{'id' : ?0}", fields = "{'pictureId': 1}")
public User findPictureById(ObjectId userId);
Above query return null
When I change ObjectId to a String
@Query(value= "{'id' : ?0}", fields = "{'pictureId': 1}")
public User findPictureById(String userId);
and pass ObjectId.toString() value for the parameter, it works fine i.e a non-null User Object is returned with a value for picture Id if it is present in the document and null otherwise.
Is this an expected behavior ?
Note:
I came accross this issue when I switched from 1.0.0.M3 release to a 1.0.0-BUILD-SNAPSHOT. In M3 release ObjectId as a parameter worked fine.
Log when I use ObjectId.toString as parameter value
DEBUG: org.springframework.data.mongodb.core.MongoTemplat e - findOne using query: { "_id" : { "$oid" : "4e4660d6b80e9cf8219f3c73"}} fields: { "pictureId" : 1}
Log when I use ObjectId directly as parameter value
DEBUG: org.springframework.data.mongodb.core.MongoTemplat e - findOne using query: { "_id" : { "_time" : 1313235158 , "_machine" : -1207001864 , "_inc" : 564083827 , "_new" : false}} fields: { "pictureId" : 1}
Regards
Vishnu
Regards
Vishnu
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules