1:没有实体,没有使用MongoDB
1:根据id查询
BasicDBObject queryObject = new BasicDBObject("_id",new ObjectId(""));
collection.find(queryObject);
2:指定返回的数据
public static Object findById(MongoCollection collection, String id) {
BasicDBObject queryObject = new BasicDBObject("_id",new ObjectId(""));
collection.find(queryObject);
//指定要查询的字段
BasicDBObject field = new BasicDBObject();
// field.put("intro", 1);
FindIterable findIterable = collection.find(queryObject).projection(field);
MongoCursor cursor = findIterable.iterator();
if (cursor.hasNext()){
return cursor.next();
}else {
return null;
}
}