mongoDB命令
1 //查询某一个集合下所有的数据:select * from 表名; 2 db.users.find(); 3 4 //往某一个集合中添加数据 5 db.users.insert({"uName":"张三","age":30,"gender":"男"}); 6 db.users.insert({"username":"李四","password":"123"}); 7 db.users.insert({"username":"李四","gender":"男","wife":{"wifeName":"小红","sext":"女"}}); 8 db.users.insert({"username":"王二麻子","gender":"男","wifes":[{"wifeName":"小翠"},{"wifeName":"小花"},{"wifeName":"如花","sext":"男"}]}) 9 10 //查询时以一种漂亮的格式显示 11 db.users.find().pretty(); 12 13 //分页查询 14 db.books.insert({"bookName":"Mybatis从入门到精通","price":19.9}); 15 db.books.insert({"bookName":"SpringMVC从入门到精通","price":69.9}); 16 db.books.insert({"bookName":"Spring从入门到精通","price":199.9}); 17 db.books.insert({"bookName":"MySQL从删库到跑路","price":39.9}); 18 db.books.insert({"bookName":"Python从入门到精通","price":59.9}); 19 20 //bookes集合中总共有5条数据,每页显示2条,可以分3页 21 select * from books limit 起始页码,每页显示的数据量 22 db.books.find(); 23 db.books.find().skip(4).limit(2); 24 db.books.find().limit(2).skip(4); 25 26 //带条件的查询 27 db.books.find({});//查询所有 28 //select * from books where price=19.9 29 db.books.find({"price":19.9}); 30 //select * from books where bookName='xxx' and price=xxx 31 db.books.find({"price":19.9,"bookName":"Mybatis从入门到精通"}); 32 db.books.find(); 33 34 //or:或 35 select * from books where bookName='xxx' or bookName='xxx' or price='xxx' 36 db.books.find({$or:[{"bookName" : "Mybatis从入门到精通"},{"bookName" : "MySQL从删库到跑路"},{"price" : 69.9}]}); 37 38 //<($lt) 39 db.books.find() 40 db.books.find({"price":{$lt:69.9}}); 41 //<=($lte) 42 db.books.find({"price":{$lte:69.9}}); 43 //>($gt) 44 db.books.find({"price":{$gt:69.9}}); 45 //>=($gte) 46 db.books.find({"price":{$gte:69.9}}); 47 //!=() 48 db.books.find({"price":{$ne:69.9}}); 49 50 //找出books集合中价格>=39.9 并且<=69.9 51 db.books.find({"price":{$gte:39.9,$lte:69.9}}); 52 53 //修改 54 db.books.find(); 55 //update books set price=99.9 where bookName='MySQL从删库到跑路' 56 db.books.update({"bookName" : "MySQL从删库到跑路"},{$set:{"price":99.9}}); 57 58 db.books.insert({"bookName" : "MySQL从删库到跑路", "price" : 99.9}); 59 60 db.books.update( 61 {"bookName" : "MySQL从删库到跑路"},//条件 62 {$set:{"price":9.9}}, 63 {multi:true} 64 ); 65 66 //删除 67 db.books.find(); 68 db.books.remove({"bookName" : "Spring从入门到精通"}); 69 db.books.remove({}); 70 71 72 73 74 75 Java连接数据库 76 public class MongoDBTest { 77 78 private MongoClient client = null; 79 80 private MongoDatabase database = null; 81 82 @Before 83 public void init() { 84 // 连接mongodb服务器(ip、port) 85 client = new MongoClient("127.0.0.1", 27017); 86 // 服务器--->库---->集合--->Document 87 database = client.getDatabase("dt55"); 88 } 89 //Description: 添加一条数据 90 @Test 91 public void insertOne() { 92 // 获取集合 93 MongoCollection<Document> bookCollection = database.getCollection("books"); 94 // 开始往books集合中添加数据 95 // Document document1 = new Document();// {} 96 Document document1 = new Document("bookName", "Mybatis从入门到精通");// {"bookName":"Mybatis从入门到精通"} 97 // document1.append("bookName", "Mybatis从入门到精通"); 98 document1.append("price", 29.9); 99 100 bookCollection.insertOne(document1); 101 // 最后关闭连接 102 client.close(); 103 104 } 105 //测试mongoDB的效率 106 @Test 107 public void insertOne2() { 108 long startTime = System.currentTimeMillis();// 起始时间 109 // 获取集合 110 MongoCollection<Document> bookCollection = database.getCollection("books"); 111 // 开始往books集合中添加数据 112 // Document document1 = new Document();// {} 113 114 for (int i = 0; i < 1000000; i++) { 115 Document document1 = new Document("bookName", "Mybatis从入门到精通");// {"bookName":"Mybatis从入门到精通"} 116 // document1.append("bookName", "Mybatis从入门到精通"); 117 document1.append("price", 29.9); 118 bookCollection.insertOne(document1); 119 } 120 // 最后关闭连接 121 client.close(); 122 long endTime = System.currentTimeMillis();// 起始时间 123 System.out.println("共花费" + (endTime - startTime) / 1000 + "s"); 124 125 } 126 //Description: 同时往某一个集合中添加多条数据(Document) 127 @Test 128 public void insertMany() { 129 MongoCollection<Document> bookCollection = database.getCollection("books"); 130 Document document1 = new Document("bookName", "SpringMVC从入门到精通"); 131 document1.append("price", 39.9); 132 133 Document document2 = new Document("bookName", "鸟哥的私房菜(服务器篇)"); 134 document2.append("price", 19.9); 135 136 Document document3 = new Document("bookName", "MySQL从入门到精通"); 137 document3.append("price", 99.9); 138 139 bookCollection.insertMany(Arrays.asList(document1, document2, document3)); 140 141 // 关闭资源 142 client.close(); 143 } 144 //Description: 带单个条件的删除 145 @Test 146 public void delete1() { 147 // 首先获取要删除数据的集合 148 MongoCollection<Document> bookCollection = database.getCollection("books"); 149 // 执行删除方法 150 // price=29.9 151 Bson bson1 = Filters.eq("price", 29.9);// 构建条件 152 bookCollection.deleteMany(bson1); 153 // 关闭资源 154 client.close(); 155 } 156 //Description: 带多个条件的删除 157 @Test 158 public void delete2() { 159 // 首先获取要删除数据的集合 160 MongoCollection<Document> bookCollection = database.getCollection("books"); 161 // delete from books where price>=19.9 and price<=99.9 162 Bson bson1 = Filters.gte("price", 19.9);// price>=19.9 163 Bson bson2 = Filters.lte("price", 99.9);// price<=99.9 164 Bson bson3 = Filters.and(bson1, bson2); 165 bookCollection.deleteMany(bson3); 166 167 // 关闭资源 168 client.close(); 169 } 170 //查询与修改 171 @Test 172 public void getAll() { 173 MongoCollection<Document> bookCollection = database.getCollection("books"); 174 // 查询操作 175 // 查询所有数据 176 FindIterable<Document> documentList = bookCollection.find();// List<Document> 177 for (Document document : documentList) { 178 System.out.println(document); 179 } // ssh--->ssm--->springboot 180 // 关闭资源 181 client.close();// IT-->安全 效率 操作简洁 182 } 183 //Description: 迭代器 184 @Test 185 public void getAll2() { 186 MongoCollection<Document> bookCollection = database.getCollection("books"); 187 // 查询操作 188 // 查询所有数据 189 FindIterable<Document> documentList = bookCollection.find();// List<Document> 190 MongoCursor<Document> documentCursor = documentList.iterator(); 191 while (documentCursor.hasNext()) { 192 Document document = documentCursor.next(); 193 System.out.println(document); 194 } 195 // 关闭资源 196 client.close(); 197 } 198 //Description: 带条件的查询 199 @Test 200 public void get3() { 201 MongoCollection<Document> bookCollection = database.getCollection("books"); 202 // 查询操作 203 // 查询所有数据 204 //// select * from books where price>=39.9 and price<199.9 205 Bson bson1 = Filters.and(Filters.gte("price", 39.9), Filters.lt("price", 199.9)); 206 FindIterable<Document> documentList = bookCollection.find(bson1);// List<Document> 207 for (Document document : documentList) { 208 String bookName = (String) document.get("bookName"); 209 Double price = (Double) document.get("price");// 39.900 210 // 数值格式化 Date yyyy-MM-dd 211 System.out.println(bookName + "的价格为:" + String.format("%.2f", price)); 212 } 213 // 关闭资源 214 client.close(); 215 } 216 //Description: 分页查询 217 @Test 218 public void get4() { 219 MongoCollection<Document> bookCollection = database.getCollection("books"); 220 // 查询操作 221 // 查询所有数据 222 FindIterable<Document> documentList = bookCollection.find();// List<Document> 223 // limit()、skip()方法是FindIterable中 224 FindIterable<Document> resultList = documentList.skip(2).limit(2); 225 for (Document document : resultList) { 226 System.out.println(document); 227 } 228 // 关闭资源 229 client.close(); 230 } 231 //修改 232 @Test 233 public void update1() { 234 // 1、获取集合 235 MongoCollection<Document> bookCollection = database.getCollection("books"); 236 // 2.执行修改操作 237 Bson bson1 = Filters.eq("bookName", "Python从入门到精通");// 条件 238 // {$set:{"price":99.9}} 239 Document document1 = new Document("price", 99.9);// value 240 Document document2 = new Document("$set", document1);// key-value 241 242 bookCollection.updateMany(bson1, document2); 243 244 // 关闭资源 245 client.close(); 246 } 247 //修改 248 @Test 249 public void update2() { 250 // 1、获取集合 251 MongoCollection<Document> bookCollection = database.getCollection("books"); 252 // 2.执行修改操作 253 Bson bson1 = Filters.eq("bookName", "Python从入门到精通");// 条件 254 // {$set:{"price":99.9}} 255 Document document1 = new Document("price", 99.9);// value 256 Document document2 = new Document("$set", document1);// key-value 257 258 bookCollection.updateMany(bson1, document2); 259 260 // 关闭资源 261 client.close(); 262 }
posted on 2019-01-21 10:35 长相思兮长相忆ing 阅读(133) 评论(0) 编辑 收藏 举报