mongodb java增删该查和模糊、排序和分页查询

package mongodb;


import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;


import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.QueryOperators;
import com.mongodb.ServerAddress;
import com.mongodb.WriteResult;


public class test {


public static void main(String[] args) throws Exception, MongoException {
System.out.println("start>>>>>>>>>>>>");
//使用哪个方法打开哪个
//add();
//del();
//update();
//test3();
test4();
//delAll();
//queryPage();
//test5();
//test6();
//test7();
System.out.println("end>>>>>>>>>>>>>>");
}
//查询个数。
public static void test7() throws Exception, MongoException{
Mongo mg = new Mongo();
DB db = mg.getDB("test");
DBCollection test1 = db.getCollection("test1");
DBCursor cursor = test1.find();
System.out.println("总个数count"+cursor.count());
System.out.println("总个数size"+cursor.size());
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}
//in ,not in查询
public static void test6()  throws Exception, MongoException{
Mongo mg = new Mongo();
DB db = mg.getDB("test");
DBCollection test1 = db.getCollection("test1");
DBObject obj = new BasicDBObject();
//QueryOperators.IN 属于   ,QueryOperators.NIN 不属于
obj.put("sort",new BasicDBObject(QueryOperators.IN, new int[]{5,6,7}));
DBCursor cursor = test1.find(obj);
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}
//大于,小于查询
public static void test5()  throws Exception, MongoException{
Mongo mg = new Mongo();
DB db = mg.getDB("test");
DBCollection test1 = db.getCollection("test1");
DBObject obj = new BasicDBObject();
//lte:小于等于, gte:大于等于 ,ne:不等于
obj.put("sort", new BasicDBObject("$lte",5));
DBCursor cursor = test1.find(obj);
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}
//分页
public static void queryPage() throws Exception, MongoException{
Mongo mg = new Mongo();
DB db = mg.getDB("test");
DBCollection test1 = db.getCollection("test1");
//limit 页数  skip 开始
DBCursor cursor = test1.find().limit(10).skip(0).sort(new BasicDBObject("sort",1));
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}
//删除集合所有元素
public static void delAll() throws Exception, MongoException{
Mongo mg = new Mongo();
DB db = mg.getDB("test");
DBCollection test1 = db.getCollection("test1");
test1.remove(new BasicDBObject());
DBCursor cursor = test1.find();
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}
//倒序查询
public static void test4() throws Exception, MongoException{
Mongo mg = new Mongo();
DB db = mg.getDB("test");
DBCollection test1 = db.getCollection("test1");
DBObject dbo = new BasicDBObject();
dbo.put("sort", -1);
DBCursor cursor = test1.find().sort(dbo);
while(cursor.hasNext()){
System.out.println(cursor.next());
}
}
//模糊查询
public static void test3() throws Exception, MongoException{
Mongo mg = new Mongo();
DB db = mg.getDB("test");
DBCollection test1 = db.getCollection("test1");
DBObject dbo = new BasicDBObject();
Pattern pattern = Pattern.compile("^.*angchunyu.*$",Pattern.CASE_INSENSITIVE);
dbo.put("username", pattern);
dbo.put("password", "123456");
DBCursor cursor = test1.find(dbo);
//多少个
System.out.println(cursor.size());
while (cursor.hasNext()) {
DBObject obj = cursor.next();
System.out.println(obj);
}
}
//添加信息
public static void add() throws Exception, MongoException{
Mongo mg = new Mongo();
DB db = mg.getDB("test");
DBCollection test1 = db.getCollection("test1");
DBObject jo = new BasicDBObject();
jo.put("username", "xuxu");
jo.put("password", "123456");
jo.put("unitname", "中天国华");
jo.put("enabled", true);
jo.put("sort",8);
jo.put("sex", "女");
test1.save(jo);
DBCursor cursor = test1.find();
while (cursor.hasNext()) {
DBObject obj = cursor.next();
System.out.println(obj);
}
}
//删除
public static void del() throws Exception, MongoException{
Mongo mg = new Mongo();
DB db = mg.getDB("test");
DBCollection test1 = db.getCollection("test1");
DBObject o = new BasicDBObject();
o.put("username", "zhangfei");
WriteResult num = test1.remove(o);
System.out.println(num);
DBCursor cursor = test1.find();
while (cursor.hasNext()) {
DBObject obj = cursor.next();
System.out.println(obj);
}
}
//修改
public static void update() throws Exception, MongoException{
Mongo mg = new Mongo();
DB db = mg.getDB("test");
DBCollection test1 = db.getCollection("test1");
DBObject oldobj = new BasicDBObject();
oldobj.put("username", "zhangfei");
DBObject newobj = test1.findOne(oldobj);
newobj.put("username", "zhangfeinew");
WriteResult num = test1.update(oldobj,newobj);
System.out.println(num.toString());
DBCursor cursor = test1.find();
while (cursor.hasNext()) {
DBObject obj = cursor.next();
System.out.println(obj);
}
}
//测试
public static void test1() throws Exception, MongoException{
//连接本地mongodb 默认为 127.0.0.1 端口为27017
Mongo mg = new Mongo();
/*DB db = mg.getDB("test");
DBCollection users = db.getCollection("users");
DBCursor cur = users.find();
System.out.println(users.count());
while(cur.hasNext()){
System.out.println(cur.next());
}*/
//版本号
String version = mg.getVersion();
System.out.println(version);
//数据库名称
List dbnames = mg.getDatabaseNames();
System.out.println(dbnames);
//访问地址
ServerAddress address = mg.getAddress();
System.out.println(address);
//连接test数据库
DB db = mg.getDB("test");
//集合名称
Set collectionnames = db.getCollectionNames();
System.out.println(collectionnames);
//连接集合
DBCollection mycol = db.getCollection("mycol");

DBObject object1 = new BasicDBObject();
object1.put("name", "chunyu1");
object1.put("title", "title");
List list = new ArrayList();
list.add(1);
list.add(2);
list.add(3);
object1.put("list",list );
mycol.insert(object1);

DBObject object2 = new BasicDBObject();
object1.put("name", "chunyu");
object2.put("title", "title");
object2.put("list",list );

mycol.save(object1);
DBObject obj = new BasicDBObject();
obj.put("name", "chunyu");
DBCursor cursor = mycol.find();
//DBCursor cursor = mycol.find();
while(cursor.hasNext()){
System.out.println("11111>>>"+cursor.next());
/*DBObject ddd = cursor.next();
Set sets = ddd.keySet();
for (String name : sets) {
Object value = ddd.get(name);
System.out.println(">>>"+value);
}*/
}
DBObject aaa = mycol.findOne();
System.out.println(aaa);

//mycol.drop();
}

}

你可能感兴趣的:(mongodb)