0

I would like to store a Map property in my bean and want it to be stored in MongoDB using Spring Data Mongodb.

public class Sample {
   Map values;
   //constructor, getters and setters go here
}

The saving happens fine. Assume that I have saved a Mongo document like this using this bean:

{
  "values": {
     "id":123,
     "name":"Vivek"
   }
 }

Now, I would like to query back this document. So, I write something like this:

 Query.addCriteria(Criteria.where("values.id").is(123));

This results in an exception while executing the find().

java.lang.IllegalArgumentException: [Assertion failed] - this argument is required; it must not be null
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.util.Assert.notNull(Assert.java:123)
at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:69)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:327)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:307)
at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:290)
at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:274)
at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.getPath(QueryMapper.java:559)
at org.springframework.data.mongodb.core.convert.QueryMapper$MetadataBackedField.<init>(QueryMapper.java:486)
at org.springframework.data.mongodb.core.convert.QueryMapper.getMappedObject(QueryMapper.java:104)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1489)
at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:1480)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:527)
at org.springframework.data.mongodb.core.MongoTemplate.find(MongoTemplate.java:518)

How can we get out of this exception?

2 Answers 2

1

I had to change Map values; to Map<Object,Object> values; and it started working.

Sign up to request clarification or add additional context in comments.

Comments

0

try this

    Query query = new Query(Criteria.where("values._id").is(id));

    query.fields().include("values.$");

    Sample sample= mongoTemplate.findOne(query, Sample.class,"sample");

    Map values= sample.getValues();

if you still faces the same problem try changing the name values as seems like a reserved word maybe.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.