Question

I have a DAO called Foo. MongoTemplate wants to map this to the database collection named foos. The problems are:

  • In Mongo, the collection is named Foos.
  • The Foo object comes from a third-party library and all its constructors are private.

How do I configure my Spring Beans such that MongoTemplate--via a MongoItemReader--knows the collection Foos maps to my Document Foo?

If it helps, I'm primarily concerned with reading data from MongoDB.

Was it helpful?

Solution

Answer: I ended up extending MongoItemReader and adding a getter/setter for collection. This allowed me to delegate the stuff I didn't care about, and modify doPageRead() to set the collection in my custom query:

@Scope("step")
public class MyMongoItemReader extends MongoItemReader<Object> {

    public void setCollection(String collection) {
        this.collection = collection;
    }

    @Override
    protected Iterator<Object> doPageRead() {

        ...
        Query mongoQuery = // set query
        ...

        return (Iterator<Object>) //
           template.find(mongoQuery, Object.class, this.collection).iterator();
    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top