MongoDB低级查询
MongoDB低级查询
1 AND操作
01 @Test 02 public void testAnd(){ 03 //agender='female' AND age > 27 04 DBObject queryCondition = new BasicDBObject(); 05 queryCondition.put("agender", "female"); 06 queryCondition.put("age", new BasicDBObject("$gt", 27)); 07 DBCursor dbCursor = coll.find(queryCondition); 08 assertEquals(1, dbCursor.size()); 09 assertEquals("Jane", dbCursor.next().get("username")); 10 } 2 单个字段的OR操作 01 @Test 02 public void testOrSingleField(){ 03 DBObject queryCondition = new BasicDBObject(); 04 //age<15 OR age>27 05 queryCondition = new BasicDBObject(); 06 BasicDBList values = new BasicDBList(); 07 values.add(new BasicDBObject("age", new BasicDBObject("$gt", 27))); 08 values.add(new BasicDBObject("age", new BasicDBObject("$lt", 15))); 09 queryCondition.put("$or", values); 10 11 DBCursor dbCursor = coll.find(queryCondition); 12 assertEquals(3, dbCursor.size()); 13 assertEquals("tom", dbCursor.next().get("username")); 14 } 3 多个字段的OR操作 01 @Test 02 public void testOrMultiFields(){ 03 DBObject queryCondition = new BasicDBObject(); 04 //agender=female OR age<=23 05 queryCondition = new BasicDBObject(); 06 BasicDBList values = new BasicDBList(); 07 values.add(new BasicDBObject("agender", "female")); 08 values.add(new BasicDBObject("age", new BasicDBObject("$lte", 23))); 09 queryCondition.put("$or", values); 10 11 DBCursor dbCursor = coll.find(queryCondition); 12 assertEquals(4, dbCursor.size()); 13 assertEquals("Jim", dbCursor.next().get("username")); 14 } 4 IN操作 01 @Test 02 public void testIn(){ 03 DBObject queryCondition = new BasicDBObject(); 04 //age in [13, 47] 05 queryCondition = new BasicDBObject(); 06 BasicDBList values = new BasicDBList(); 07 values.add(13); 08 values.add(47); 09 queryCondition.put("age", new BasicDBObject("$in", values)); 10 11 DBCursor dbCursor = coll.find(queryCondition); 12 assertEquals(2, dbCursor.size()); 13 assertEquals("tom", dbCursor.next().get("username")); 14 } 5 查询 name <> "li" and age >= 18的数据 1 BasicDBObject query = new BasicDBObject(); 2 query.put("name", new BasicDBObject().append("$ne", "liguohui")); 3 query.put("age", new BasicDBObject().append("$gte", 18)); 4 coll.find(query) 6 查询 age> 10 and age<= 30的数据 1 BasicDBObject query = new BasicDBObject(); 2 query.put("age", new BasicDBObject().append("$gt", 10)); 3 query.put("age", new BasicDBObject().append("$lte", 30)); 4 coll.find(query); 7 查询 age in (20,22,24)的数据 1 BasicDBObject query = new BasicDBObject(); 2 BasicDBList values = new BasicDBList(); 3 values.add(20); 4 values.add(22); 5 values.add(24); 6 query.put("age", new BasicDBObject("$in", values)); 7 DBCursor cur = coll.find(query); 8 以年龄升序asc 1 DBCursor cur = coll.find().sort(new BasicDBObject().append("name", 1)); 9 以年龄降序desc 1 DBCursor cur = coll.find().sort(new BasicDBObject().append("name", -1)); 10 查询记录条数count() 1 BasicDBObject query = new BasicDBObject(); 2 query.put("age", new BasicDBObject("$lge", 10)); 3 long count = coll.getCount(query); 11
01 public boolean fileExistByUserId(String md5, Integer userId) { 02 boolean isExist = false; 03 try { 04 db = MongoDBConnection.getDB(); 05 dbColleciton = db.getCollection("fileInfoCollection"); 06 BasicDBObject query = new BasicDBObject(); 07 query.put("fileMD5", md5); 08 query.put("userId", userId); 09 BasicDBList dbList = new BasicDBList(); 10 dbList.add(0); 11 dbList.add(4); 12 dbList.add(FileInfoModelExtra.USERPRIVATE); 13 query.put("fileStatus", new BasicDBObject("$in", dbList)); 14 DBCursor cursor = dbColleciton.find(query); 15 if (cursor != null) { 16 // isExist = cursor.count() > 0 ? true : false; 17 isExist = cursor.iterator().hasNext() ? true : false; 18 } 19 20 } catch (MongoException e) { 21 logger.info(e); 22 } catch (RuntimeException e) { 23 logger.info(e.getMessage()); 24 } catch (Exception e) { 25 logger.info(e.getMessage()); 26 } 27 return isExist; 28 }