如何使用java检索此doc

时间:2014-04-14 18:03:19

标签: java json mongodb rest

任何人都可以帮助我从mongodb

检索这种类型的json身体文件
{
"question":"what is your color?",
"choices":[{"option":"yello"},{"option":"blue"},{"option":"green"}],
"creation-date":"2014-04-13",
"expiry date":"2014-04-14"
}

这是我正在编写的代码

   public Poll getPollById(String id) {
    try{
    mongoClient=new MongoClient("NavDeep",27017);
    db=mongoClient.getDB("sms-voting");
    collection=db.getCollection("pollsCollection");
    }catch(Exception e){
    e.printStackTrace();
    }

    Poll poll=new Poll();
    Choice c=new Choice();
    List<Choice> choice=new ArrayList<Choice>(); 
    DBObject object=collection.findOne(new BasicDBObject("pollid",id));
    String pollID=(String)object.get("pollid");
    String question=(String)object.get("question");
    //how should i retrive choices??
    BasicDBList choices=(BasicDBList) object.get("choices");


    for(int i=0;i<choices.size();i++){
    BasicDBObject choicesObj = (BasicDBObject) choices.get(i);
    String opt=choicesObj.getString("option");
    long count=choicesObj.getLong("count");
    c.setOption(opt);
    c.setCount(count);
    choice.add(c);

    }
    Date creation_date=(Date)object.get("creation date");
    Date expiry_date=(Date)object.get("expiry date");

    poll.setId(pollID);
    poll.setQuestion(question);
    poll.setChoices(choice);
    poll.setStart(creation_date);
    poll.setEnd(expiry_date);
    mongoClient.close();
    return poll;
    }

这些是Poll和Choice类的类     包edu.sjsu.cmpe.voting.api;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;

import edu.sjsu.cmpe.voting.api.Choice;

public class Poll {


@JsonProperty("pollid")
@JsonInclude(Include.NON_DEFAULT)
private String id;
@JsonProperty("question")
private String question;

@Valid
@JsonProperty("choices")
private ArrayList<Choice> choices;

@JsonProperty("creation date")
private Date start;

@JsonProperty("expiry date")
private Date end;   

public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
/**
 * @return the question
 */
public String getQuestion() {
    return question;
}
/**
 * @param question the question to set
 */
public void setQuestion(String question) {
    this.question = question;
}

public ArrayList<Choice> getChoices() {
    return choices;
}
public void setChoices(ArrayList<Choice> choices) {

    this.choices=choices;
}

public Date getStart() {
    return start;
}
public void setStart(Date start) {
    this.start = start;
}
public Date getEnd() {
    return end;
}
public void setEnd(Date end) {
    this.end = end;
}

}

其中Choice是另一个具有选项和计数字段的类。

我收到BasicDBobject(get("choices")) cnanot be converted into BasicDBList

的例外情况

怎么做??

1 个答案:

答案 0 :(得分:0)

该错误意味着对于您正在检索的文档,&#34;选择&#34; field是一个对象而不是一个数组。因此,请使用mongo shell并验证您查询数据的ID是否正确,如您所愿。