mongodb 笔记
1.java驱动下的mongo操作
1.1 数据库连接
1 package com.mongodb.text; 2 3 import java.net.UnknownHostException; 4 5 import com.mongodb.DB; 6 import com.mongodb.DBCollection; 7 import com.mongodb.DBCursor; 8 import com.mongodb.DBObject; 9 import com.mongodb.Mongo; 10 import com.mongodb.MongoException; 11 import com.mongodb.util.JSON; 12 13 public class DataBase { 14 public static void main(String[] args) 15 throws UnknownHostException, MongoException { 16 //1.建立一个Mongo的数据库连接对象 17 Mongo mg = new Mongo("127.0.0.1:27017"); 18 //查询所有的Database 19 for (String name : mg.getDatabaseNames()) { 20 System.out.println("dbName: " + name); 21 } 22 //2.创建相关数据库的连接 23 DB db = mg.getDB("foobar"); 24 //查询数据库所有的集合 25 for (String name : db.getCollectionNames()) { 26 System.out.println("collectionName: " + name); 27 } 28 29 DBCollection users = db.getCollection("persons"); 30 //查询所有的数据 31 DBCursor cur = users.find(); 32 while (cur.hasNext()) { 33 DBObject object = cur.next(); 34 System.out.println(object.get("name")); 35 } 36 System.out.println(cur.count()); 37 System.out.println(cur.getCursorId()); 38 System.out.println(JSON.serialize(cur)); 39 } 40 }
1.2 数据库常见操作
1 package com.mongodb.text; 2 3 import java.net.UnknownHostException; 4 import java.util.List; 5 6 import org.bson.types.ObjectId; 7 8 import com.mongodb.BasicDBObject; 9 import com.mongodb.DB; 10 import com.mongodb.DBCollection; 11 import com.mongodb.DBCursor; 12 import com.mongodb.DBObject; 13 import com.mongodb.Mongo; 14 import com.mongodb.MongoException; 15 16 public class MongoDb { 17 //1.建立一个Mongo的数据库连接对象 18 static Mongo connection = null; 19 //2.创建相关数据库的连接 20 static DB db = null; 21 public MongoDb(String dbName) throws UnknownHostException, MongoException{ 22 connection = new Mongo("127.0.0.1:27017"); 23 db = connection.getDB(dbName); 24 } 25 public static void main(String[] args) throws UnknownHostException, MongoException { 26 //实例化 27 MongoDb mongoDb = new MongoDb("foobar"); 28 /** 29 * 1.创建一个名字叫javadb的数据库 30 */ 31 // mongoDb.createCollection("javadb"); 32 /** 33 * 2.为集合javadb添加一条数据 34 */ 35 // DBObject dbs = new BasicDBObject(); 36 // dbs.put("name", "uspcat.com"); 37 // dbs.put("age", 2); 38 // List<String> books = new ArrayList<String>(); 39 // books.add("EXTJS"); 40 // books.add("MONGODB"); 41 // dbs.put("books", books); 42 // mongoDb.insert(dbs, "javadb"); 43 /** 44 * 3.批量插入数据 45 */ 46 // List<DBObject> dbObjects = new ArrayList<DBObject>(); 47 // DBObject jim = new BasicDBObject("name","jim"); 48 // DBObject lisi = new BasicDBObject("name","lisi"); 49 // dbObjects.add(jim); 50 // dbObjects.add(lisi); 51 // mongoDb.insertBatch(dbObjects, "javadb"); 52 /** 53 * 4.根据ID删除数据 54 */ 55 // mongoDb.deleteById("502870dab9c368bf5b151a04", "javadb"); 56 /** 57 * 5.根据条件删除数据 58 */ 59 // DBObject lisi = new BasicDBObject(); 60 // lisi.put("name", "lisi"); 61 // int count = mongoDb.deleteByDbs(lisi, "javadb"); 62 // System.out.println("删除数据的条数是: "+count); 63 /** 64 * 6.更新操作,为集合增加email属性 65 */ 66 // DBObject update = new BasicDBObject(); 67 // update.put("$set", 68 // new BasicDBObject("eamil","uspcat@126.com")); 69 // mongoDb.update(new BasicDBObject(), 70 // update,false,true,"javadb"); 71 /** 72 * 7.查询出persons集合中的name和age 73 */ 74 // DBObject keys = new BasicDBObject(); 75 // keys.put("_id", false); 76 // keys.put("name", true); 77 // keys.put("age", true); 78 // DBCursor cursor = mongoDb.find(null, keys, "persons"); 79 // while (cursor.hasNext()) { 80 // DBObject object = cursor.next(); 81 // System.out.println(object.get("name")); 82 // } 83 /** 84 * 8.查询出年龄大于26岁并且英语成绩小于80分 85 */ 86 // DBObject ref = new BasicDBObject(); 87 // ref.put("age", new BasicDBObject("$gte",26)); 88 // ref.put("e", new BasicDBObject("$lte",80)); 89 // DBCursor cursor = mongoDb.find(ref, null, "persons"); 90 // while (cursor.hasNext()) { 91 // DBObject object = cursor.next(); 92 // System.out.print(object.get("name")+"-->"); 93 // System.out.print(object.get("age")+"-->"); 94 // System.out.println(object.get("e")); 95 // } 96 /** 97 * 9.分页例子 98 */ 99 DBCursor cursor = mongoDb.find(null, null, 0, 3, "persons"); 100 while (cursor.hasNext()) { 101 DBObject object = cursor.next(); 102 System.out.print(object.get("name")+"-->"); 103 System.out.print(object.get("age")+"-->"); 104 System.out.println(object.get("e")); 105 } 106 //关闭连接对象 107 connection.close(); 108 } 109 /** 110 * 穿件一个数据库集合 111 * @param collName 集合名称 112 * @param db 数据库实例 113 */ 114 public void createCollection(String collName){ 115 DBObject dbs = new BasicDBObject(); 116 db.createCollection("javadb", dbs); 117 } 118 /** 119 * 为相应的集合添加数据 120 * @param dbs 121 * @param collName 122 */ 123 public void insert(DBObject dbs,String collName){ 124 //1.得到集合 125 DBCollection coll = db.getCollection(collName); 126 //2.插入操作 127 coll.insert(dbs); 128 } 129 /** 130 * 为集合批量插入数据 131 * @param dbses 132 * @param collName 133 */ 134 public void insertBatch(List<DBObject> dbses,String collName){ 135 //1.得到集合 136 DBCollection coll = db.getCollection(collName); 137 //2.插入操作 138 coll.insert(dbses); 139 } 140 /** 141 * 根据id删除数据 142 * @param id 143 * @param collName 144 * @return 返回影响的数据条数 145 */ 146 public int deleteById(String id,String collName){ 147 //1.得到集合 148 DBCollection coll = db.getCollection(collName); 149 DBObject dbs = new BasicDBObject("_id", new ObjectId(id)); 150 int count = coll.remove(dbs).getN(); 151 return count; 152 } 153 /** 154 * 根据条件删除数据 155 * @param id 156 * @param collName 157 * @return 返回影响的数据条数 158 */ 159 public int deleteByDbs(DBObject dbs,String collName){ 160 //1.得到集合 161 DBCollection coll = db.getCollection(collName); 162 int count = coll.remove(dbs).getN(); 163 return count; 164 } 165 /** 166 * 更新数据 167 * @param find 查询器 168 * @param update 更新器 169 * @param upsert 更新或插入 170 * @param multi 是否批量更新 171 * @param collName 集合名称 172 * @return 返回影响的数据条数 173 */ 174 public int update(DBObject find, 175 DBObject update, 176 boolean upsert, 177 boolean multi, 178 String collName){ 179 //1.得到集合 180 DBCollection coll = db.getCollection(collName); 181 int count = coll.update(find, update, upsert, multi).getN(); 182 return count; 183 } 184 /** 185 * 查询器(分页) 186 * @param ref 187 * @param keys 188 * @param start 189 * @param limit 190 * @return 191 */ 192 public DBCursor find(DBObject ref, 193 DBObject keys, 194 int start, 195 int limit, 196 String collName){ 197 DBCursor cur = find(ref, keys, collName); 198 return cur.limit(limit).skip(start); 199 } 200 /** 201 * 查询器(不分页) 202 * @param ref 203 * @param keys 204 * @param start 205 * @param limit 206 * @param collName 207 * @return 208 */ 209 public DBCursor find(DBObject ref, 210 DBObject keys, 211 String collName){ 212 //1.得到集合 213 DBCollection coll = db.getCollection(collName); 214 DBCursor cur = coll.find(ref, keys); 215 return cur; 216 } 217 }