一、什么是Jongo
jongo的主要作用是在java程序中使用mongo shell命令
二、jongo的使用
1、需要的包有
(1) bson4jackson-2.2.3.jar
(2)jackson-annotations-2.3.3.jar
(3)jackson-core-2.3.3.jar
(4)jackson-databind-2.2.3.jar
(5)jongo-1.0.jar
(6)mongo-java-driver-2.10.1.jar
jongo依赖jackson和bson4jackson两个包
2、使用
package testjson;
import java.net.UnknownHostException;
import org.jongo.FindOne;
import org.jongo.Jongo;
import org.jongo.MongoCollection;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import entity.Student;
/**
* @author Administrator
*测试使用jongo
*/
public class TestJongo {
Jongo jongo = null;
MongoCollection col = null;
@Before
public void beforeTest(){
DB db;
try {
db = new MongoClient().getDB("mydata");
jongo = new Jongo(db);
col = jongo.getCollection("mytest");
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//查找
@Test
public void testJongoRead(){
Iterable<Student> iterables = col.find("{name:'zhangsan'}").as(Student.class);
for(Student stu:iterables){
System.out.println(stu);
}
Student one = col.findOne("{name:'zhangsan1'}").as(Student.class);
System.out.println(one);
}
//插入
@Test
public void testJongoWrite(){
Student student = new Student();
student.setNum("112");
student.setName("zhangkeke");
student.setAge(20);
col.save(student);
}
/**
* #表示占位符,动态添加参数 动态参数的添加支持类如col.find("{address;#}",new Address(....))
*
*/
@Test
public void testJongoQuery(){
Student student = new Student();
student.setName("zhangkeke");
Student s = col.findOne("{name:#,age:#}","zhangkeke",24).as(Student.class);
System.out.println(s);
}
//修改
@Test
public void testJongoUpdate(){
col.update("{name:#}", "zhangkeke").upsert().multi().with("{$inc:{age:#}}",1);
testJongoQuery();
}
}
to be continue.......