2

I have an entity ProjectCycle mapped to mongo DB collection ProjectCycle. I am trying to retrieve 2 fields, _id and Status. I am able to retrieve both like the following

@Document(collection="ProjectCycle")
public class ProjectCycle {
    @Id
    private String id;
    @Field("Status")
    private String status;
//getters and setters
}

Application.java

    Query query = new Query();
    query.fields().include("Status");
    Criteria criteria = new Criteria();
    criteria.and("_id").is("1000");
    query.addCriteria(criteria);
    Iterable<ProjectCycle> objectList = mongoOperations.find(query, ProjectCycle.class);
    for(ProjectCycle obj : objectList) {
        System.out.println("_id "+obj.getId());
        System.out.println("status "+obj.getStatus());
    }

Output

_id 1000
status Approved

But, the problem is when i use an Entity with field private DBObject basicDbObject; instead of private String status; i am getting value as null instead of Approved

I have tried like the following

public class ProjectCycle {
    @Id
    private String id;
    private DBObject basicDbObject;
    //getter & setter
}

What I am trying to achieve is that, the collection 'ProjectCycle' is very large and creating a POJO corresponding to it is quiet difficult. Also I am only reading data from mongoDB. So creating the entire POJO is time wasting and tedious.

  • How I can achieve mapping between any field/fields from mongo Collection to entity?.
  • Will it be possible to create a Map<String, BasicDBObject> objectMap; to fields returned from query? I am using Spring-data-mongodb for the same.

Version details

Spring 4.0.7.RELEASE

spring-data-mongodb 1.7.2.RELEASE

1 Answer 1

2

Try mapping your query like below.

Iterable<BasicDBObject> objectList = mongoOperations.find(query, BasicDBObject.class, collectionname);

for(BasicDBObject obj : objectList) {
    System.out.println("_id "+obj.get("id"));
    System.out.println("status "+obj.get("status"));
}
Sign up to request clarification or add additional context in comments.

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.