MongoDBUtils
MongoDB access and operate tools, suport use sql statement to ope mongodb (select, update, insert, delete)
普通构造
MongoDbOperater ope = new MongoDbOperater();
ope.setMongoClient(client);
spring 构造
<bean id="mongoClient" class="com.mongodb.MongoClient"\>
...
<\bean>
<bean id="ope" class="org.shenjitang.mongodbutils.MongoDbOperater">
特性:
1,支持sql语法的增删改查
2,支持原生mongodb操作
3,自动转换bean
4,支持GridFS操作
使用(假设数据库名为 db1):
String sql = "select * from colName where name='tom' and age>15 and score between (80, 100) and city in ("shanghai", "beijing", "guangzhou");
Persion persion = ope.findOneObj("db1", sql, Persion.class);
List persionList = ope.findAllObj("db1", sql, Persion.class);
...
支持的sql样例:
sql = "select * from table1 where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3') order by age,name limit 123";
sql = "select * from algoflow_instance_log where functionName='f1' and reuseResult=false and returnCode=0 and action='LEAVE' order by timestamp desc limit 100";
更新:
sql = "update table1 set a=1, b='sdaf', c='2012-11-23 17:45:32' where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3')";,
ope.update("db1", sql);
sql = "delete from table1 where name='aaa' and age>=18 and foot in (1,2,3) and abc in ('a1','a2','a3') order by age,name limit 123";
ope.remove("db1", sql);
以上sql都可以执行:QueryInfo uinfo = ope.sql2QueryInfo("db1", sql);