Announcement Announcement Module
No announcement yet.
Using an Enum in a where clause in mongodb Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using an Enum in a where clause in mongodb

    Hi There I am trying to use an enum as a match value in a where clause for mongodb with spring data like this:

    public List<HitCount> findPopular(EntityType entityType, int limit) {
    		Query q = query(where("entityType").is(entityType)).limit(limit);
    		q.sort().on("hits", Order.DESCENDING);
    		return mongoTemplate.find(q, HitCount.class);
    where EnityType is an enum.

    My basic config is as follows so i am using the mapping converter:
    <mongo:mongo host="" port="27017" />
    	<mongo:db-factory dbname="${dbName}" mongo-ref="mongo"/>
    	<mongo:repositories base-package="com.excira" />
    	<mongo:mapping-converter base-package="com.excira" id="mappingConverter"/>
    	<bean id="mongoTemplate" class="com.excira.ute.dao.mongo.UTEMongoTemplate">
    	  <constructor-arg ref="mongoDbFactory"/>
       	  <constructor-arg ref="mappingConverter"/>
    		<!-- property name="username" value="${userName}" / -->
    		<!-- property name="password" value="${password}" / -->
    I have 2 problems as described below, any help would be appreciated.

    1) if debug is enabled, i get an error when the template tries to print out the query in a debug string saying it cant serialize the entity. Looks like it could call name() on the enum here?

    java.lang.RuntimeException: json can't serialize type : class com.excira.muchenough.MEEntityTypes
    at com.mongodb.util.JSON.serialize(
    at com.mongodb.util.JSON.serialize(
    at com.mongodb.util.JSON.serialize(
    at com.mongodb.BasicDBObject.toString(BasicDBObject.j ava:75)
    at java.lang.String.valueOf(
    at java.lang.StringBuilder.append( 115)
    at plate.doFind(
    at plate.find(
    at plate.find(
    at ndPopular(

    2) If i disable debug in the, then while the query is being executed i get the exception below:

    java.lang.IllegalArgumentException: can't serialize class com.excira.muchenough.MEEntityTypes
    at org.bson.BSONEncoder._putObjectField(BSONEncoder.j ava:205)
    at org.bson.BSONEncoder.putObject( 1)
    at org.bson.BSONEncoder._putObjectField(BSONEncoder.j ava:171)
    at org.bson.BSONEncoder.putObject( 1)
    at org.bson.BSONEncoder.putObject( )
    at com.mongodb.OutMessage._appendQuery(OutMessage.jav a:62)
    at com.mongodb.OutMessage.query(
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLa
    at com.mongodb.DBCursor._check(
    at com.mongodb.DBCursor._hasNext(
    at com.mongodb.DBCursor.hasNext(
    at plate.executeFindMultiInternal( 1425)
    at plate.doFind(
    at plate.find(
    at plate.find(
    at ndPopular(

  • #2
    Looks like a conversion issue - there is probably no converter to handle the Enum available.

    What version of the Mongo support are you using? If you see this issue with the latest M3 version then could you file a JIRA issue here.



    • #3
      i have the same problem.. i use 1.0.0.RELEASE
      i dont think it's the converter.. it works fine in saving and retrieving the object.. it just wont work in "Where" clause.
      any ideas???