读书人

开源的Mongodb java client - mango公

发布时间: 2012-09-29 10:30:01 作者: rapoo

开源的Mongodb java client -- mango发布

Mango? ----?? 一个非常简单的操作mongodb的小工具,使用java语言,基于mongodb的java driver包。

?

其主要的灵感来自于Jongo项目,这是一个非常有创意的工具,将mongodb shell编程扩展到了java语言包内。mango主要做的工作,是重写了Jongo的一些方法,使其更符合我们在开发中的需求,另外与spring相结合,将配置参数等记录在资源文件中进行管理,并适当的做了一些扩展。

?

新手上路

step 1? -- 配置

资源文件? driver.property

#database url ---> host:porturl=user=password=#see mongoOptions.class in mongodb java driverslaveOk=connectionsPerHost=minPoolsSize=threadsAllowedToBlockForConnectionMultiplier=maxWaitTime=connectTimeout=socketTimeout=autoConnectRetry=safe=

?

配置文件? spring.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">    <bean id="collection" init-method="init" destroy-method="close">        <property name="mango">            <ref bean="mango"/>        </property>        <property name="collection" value="test"/>        <!-- there needs the full path of the model class , it's not a good idea , but I can't find a better way to solve this -->        <property name="clazz" value="org.mango.Mazhiyuan"/>    </bean>    <bean id="mango" init-method="init" destroy-method="close">        <property name="driver">            <ref bean="driver"/>        </property>    </bean>    <bean id="driver" init-method="init" destroy-method="close">        <property name="db" value="local"/>        <property name="driver" ref = "config"/>    </bean>    <bean id="config" name="code">ApplicationContext context = new ClassPathXmlApplicationContext("classpath:spring.xml");MangoCollection collection = (MangoCollection) context.getBean("collection");

?得到MangoCollection之后,就可以进行一系列的操作了

?

?

Mazhiyuan mazhiyuan = collection.findOne().as();System.out.println(mazhiyuan.getName());Mazhiyuan mazhiyuan = collection.findOne("{age:3}").as();List<Mazhiyuan> mazhiyuans = collection.find("{age:3}").asList();Iterable<Mazhiyuan> mazhiyuans = collection.find("{age:3}").as();List<Mazhiyuan> mazhiyuans = collection.find("{age:3}").field("{age:0}").skip(10).sort("{age:1}").limit(10).asList();
?

?这些语句和mongodb shell的语言非常的类似,注意asList是返回List对象,而as方法返回的是Iterable对象

?

collection.update("{name: 'mzy1'}").with("{$inc: {age: 1}}");collection.update("{name: 'mzy1'}").upsert().multi().with("{$inc: {age: 1}}");collection.update("{name: 'mzy1'}").upsert().with("{$inc: {age: 1}}");
?

更新操作也是一样的

?

collection.save(new Mazhiyuan("mzy", 23));collection.save("{name:'mzy',age:23}");

插入操作

?

?

collection.remove("{name: 'mzy'}");collection.remove(new ObjectId("4c...e"));

删除操作

?

需要说明的是这些操作返回都是boolean类型,可以判断操作是否成功。

?

目前,这个项目还没有完全开发完成,对group这样的操作,我本人理解也不够深刻,所以在理解group之后再将这个补充到mango中,另外,mango还没有经受过足够的测试,可能在使用中会有一些问题,请给我发邮件,或是提交bug报告。如果你有好的建议,也请告知我。

?

?

源码地址:https://github.com/mazhiyuan/mango-spring

?

读书人网 >开源软件

热点推荐