开发者

Not able to retrieve result from MongoDB

private String pageElementUpdateProperty="{'user_id':'4d9fe87d1e327f0858000003','session_token':'84146295a9c0eb344f68510ac3645763','project_id':'4dac27b6156aec840d000007','page_id':'4db90554156aec180a000005','element_id':'4dec8964206b74b0dbe2236a',property:{style:{left:177.5,'top':153.5,'width':600,'height':800}}}";

DBCollection collPageElement = getServerDocument("PageElementCollection");
JSONObject jsonPageElementObject=JSONObject.fromObject(pageElementUpdateProperty);
PageElmentBean pageElementBean = (PageElmen开发者_如何学编程tBean) JSONObject.toBean(jsonPageElementObject,PageElmentBean.class);//fill the fields of Bean with JSON object..this i'm gettig correctly

BasicDBObject query = new BasicDBObject();
query.put("_id", pageElementBean.getElement_id());
query.put("page_id", pageElementBean.getPage_id());

DBCursor cur;
cur = collPageElement.find(query);

now i have to find document using "element_id" which is "_id" in mongoDB but when i use -

query.put("_id", pageElementBean.getElement_id());

it's not getting result though document with given "page_id" and "element_id" in the string exist in mongoDB

where i'm making mistake


Is the _id a string or does it use ObjectId?

If it is an object id, you need to say:

query.put("_id", new ObjectId(pageElementBean.getElement_id()));

(same holds true with page_id - if that is an ObjectId)

You can check the type in the shell by saying:

db.YOUR_COLLECTION_NAME.findOne();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜