本文整理匯總了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();
}