本文整理汇总了Java中org.springframework.data.mongodb.core.MongoOperations.execute方法的典型用法代码示例。如果您正苦于以下问题:Java MongoOperations.execute方法的具体用法?Java MongoOperations.execute怎么用?Java MongoOperations.execute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.springframework.data.mongodb.core.MongoOperations
的用法示例。
在下文中一共展示了MongoOperations.execute方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: findDocuments
import org.springframework.data.mongodb.core.MongoOperations; //导入方法依赖的package包/类
@Override
public Page findDocuments(final DBQuery query) {
Page page=new Page();
List<DBObject> list=new ArrayList<DBObject>();
MongoOperations mo=mtContainer.getTemplateMap().get(query.getDb());
DBCursor cursor=mo.execute(query.getCollection(),new CollectionCallback<DBCursor>(){
public DBCursor doInCollection(DBCollection collection) throws MongoException, DataAccessException{
if(query.getLimit()>0) {
return collection.find(query.getCriteria(), query.getFields()).sort(query.getSort()).limit(query.getLimit());
}else return collection.find(query.getCriteria(), query.getFields()).sort(query.getSort());
}
});
while(cursor.hasNext()){
DBObject dbobj=cursor.next();
if(page.getFirstId()==null)page.setFirstId(dbobj.get("_id").toString());
if(!cursor.hasNext())page.setLastId(dbobj.get("_id").toString());
list.add(dbobj);
}
page.setItems(list);
page.setMaxSize(cursor.count());
page.setSize(list.size());
return page;
}
示例2: getDocumentCount
import org.springframework.data.mongodb.core.MongoOperations; //导入方法依赖的package包/类
@Override
public long getDocumentCount(DBQuery query) {
MongoOperations mo=mtContainer.getTemplateMap().get(query.getDb());
long count=mo.execute(query.getCollection(),new CollectionCallback<Long>(){
public Long doInCollection(DBCollection collection) throws MongoException, DataAccessException{
return collection.count();
}
});
return count;
}
示例3: getIndexInfo
import org.springframework.data.mongodb.core.MongoOperations; //导入方法依赖的package包/类
@Override
public List<DBObject> getIndexInfo(DBQuery query) {
MongoOperations mo=mtContainer.getTemplateMap().get(query.getDb());
List<DBObject> indexes=mo.execute(query.getCollection(),new CollectionCallback<List<DBObject>>(){
public List<DBObject> doInCollection(DBCollection collection) throws MongoException, DataAccessException{
return collection.getIndexInfo();
}
});
return indexes;
}
示例4: findSingleDocument
import org.springframework.data.mongodb.core.MongoOperations; //导入方法依赖的package包/类
@Override
public DBObject findSingleDocument(final DBQuery query) {
MongoOperations mo=mtContainer.getTemplateMap().get(query.getDb());
DBObject dbObject=mo.execute(query.getCollection(),new CollectionCallback<DBObject>(){
public DBObject doInCollection(DBCollection collection) throws MongoException, DataAccessException{
return collection.findOne(query.getCriteria(),query.getFields());
}
});
return dbObject;
}
示例5: getDbSchema
import org.springframework.data.mongodb.core.MongoOperations; //导入方法依赖的package包/类
@Override
public byte[] getDbSchema(String db) throws Exception {
//TODO: Not the most elegant solution. Needs cleanup
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
//create the root element and add it to the document
Element root = doc.createElement("map");
root.setAttribute("version", "0.9.0");
doc.appendChild(root);
//create a comment and put it in the root element
Comment comment = doc.createComment("To view this file, download free mind mapping software FreeMind from http://freemind.sourceforge.net");
root.appendChild(comment);
//Database node
Element node = doc.createElement("node");
node.setAttribute("BACKGROUND_COLOR", "#ffffff");
node.setAttribute("TEXT", db);
root.appendChild(node);
MongoOperations mo=mtContainer.getTemplateMap().get(db);
for(String colName:mo.getCollectionNames()){
if (!colName.equals("system.indexes")) {
if (LOG.isDebugEnabled())LOG.debug("COLLECTION: " + colName);
DBObject dbObject = mo.execute(colName,
new CollectionCallback<DBObject>() {
public DBObject doInCollection(
DBCollection collection)
throws MongoException, DataAccessException {
return collection.findOne();
}
});
String json = JSON.serialize(dbObject);
if (LOG.isDebugEnabled())LOG.debug(json);
@SuppressWarnings("unchecked")
Map<String, Object> documentData = mapper.readValue(json.getBytes(), Map.class);
Element colNode = createNode(doc, colName);
for (String key : documentData.keySet()) {
addAttribute(doc, colNode, key, documentData.get(key));
}
node.appendChild(colNode);
}
}
//Output the XML
TransformerFactory transfac = TransformerFactory.newInstance();
Transformer trans = transfac.newTransformer();
trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
trans.setOutputProperty(OutputKeys.INDENT, "yes");
//create string from xml tree
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
trans.transform(source, result);
String xmlString = sw.toString();
if (LOG.isDebugEnabled())LOG.debug("Here's the xml:\n\n" + xmlString);
return xmlString.getBytes();
}