代码之家  ›  专栏  ›  技术社区  ›  Feroz Siddiqui

MunGDB——如何使用Java对集合中的多个字段进行排序和搜索

  •  1
  • Feroz Siddiqui  · 技术社区  · 7 年前

    = i am trying to implement a method in which i create a hashmap of key as string and value as object.使用此hashmap,我需要搜索和排序Mongo中存在的数据集合。

    以下是mongo db文件的id类:

    公共类OID{ 字符串$ OID; 公共字符串get$oid()。{ 返回$OID; } public void set$oid(字符串$oid){ this.$oid=$oid; } } < /代码>

    下面是POJO Java文件:

    公共类appuser{ 私有字符串密码; 私人字符串电子邮件; 私人创建日期: 私人日期更新 类风湿性关节炎; 公共appuser()。{ 超级(); //TODO自动生成的构造函数存根 } 公共应用程序用户(字符串密码、字符串电子邮件、创建日期、更新日期){ 超级(); this.password=密码; this.email=电子邮件; this.created_at=创建时间: this.update_at=更新_at; } 公共字符串getpassword()。{ 返回密码; } public void setpassword(字符串密码){ this.password=密码; } 公共字符串getemail()。{ 回复邮件; } 公共void setemail(字符串email){ this.email=电子邮件; } 公共日期getCreated_at()。{ 退货创建时间: }

    public class Oid{
                String $oid;
                public String get$oid() {
                    return $oid;
                }
    
                public void set$oid(String $oid) {
                    this.$oid = $oid;
                }
            }
    

    public class AppUser {
      private String password;
      private String email;
      private Date created_at;
      private Date update_at;
      Oid _id;
    
      public AppUser() {
       super();
       // TODO Auto-generated constructor stub
      }
      public AppUser(String password, String email, Date created_at, Date update_at) {
       super();
       this.password = password;
       this.email = email;
       this.created_at = created_at;
       this.update_at = update_at;
      }
      public String getPassword() {
       return password;
      }
      public void setPassword(String password) {
       this.password = password;
      }
      public String getEmail() {
       return email;
      }
      public void setEmail(String email) {
       this.email = email;
      }
      public Date getCreated_at() {
       return created_at;
      }
      public void setCreated_at(Date created_at) {
       this.created_at = created_at;
      }
      public Date getUpdate_at() {
       return update_at;
      }
      public void setUpdate_at(Date update_at) {
       this.update_at = update_at;
      }
      public Oid get_id() {
       return _id;
      }
      public void set_id(Oid _id) {
       this._id = _id;
      }
     }
    

    public class AppUserDao {
     public List < AppUser > findMulitple(HashMap < String, Object > map) {
      List < AppUser > appUsers = new ArrayList < > ();
    
      MongoDatabase db = getDB();
      BasicDBObject query = new BasicDBObject(map.keySet().iterator().next(), map.get(map.keySet().toArray()[0]));
      int i = 0;
      for (String key: map.keySet()) {
       if (i != 0) {
        query.append(key, map.get(key));
       }
       i++;
      }
    
      FindIterable < Document > filter = db.getCollection("sampleCollection").find(query);
      MongoCursor < Document > cursor = filter.iterator();
      try {
       String obj = cursor.next().toJson();
       System.out.println(obj);
       AppUser appUser = gson.fromJson(obj, AppUser.class);
       appUsers.add(appUser);
      } catch (JsonSyntaxException jse) {
       jse.printStackTrace();
      } catch (Exception e) {
       e.printStackTrace();
      } finally {
       cursor.close();
      }
      return appUsers;
     }
    
    }
    

    public class TestAppUser {
      public static void main(String args[]) {
    
    
       HashMap < String, Object > map = new HashMap < > ();
       map.put("email", "berry.sanford@yahoo.com");
       map.put("password", "Quo Lux");
    
    
       AppUserDao appUserDao = new AppUserDao();
       List < AppUser > appUsers = appUserDao.findMulitple(map);
       for (AppUser appUser: appUsers) {
        System.out.println(appUser.get_id().get$oid());
       }
      }
     }
    

    1 回复  |  直到 7 年前
        1
  •  2
  •   s7vr    7 年前

    我将构建查询过滤器并按如下所示对订单进行排序。

    List<Bson> filters = map.entrySet().stream().map(entry->Filters.eq(entry.getKey(), entry.getValue())).collect(Collectors.toList());
    List<Bson> sorts = map.keySet().stream().map(Sorts::ascending).collect(Collectors.toList());
    FindIterable<Document> filter = db.getCollection("sampleCollection").find(Filters.and(filters)).sort(Sorts.orderBy(sorts));
    MongoCursor<Document> cursor = filter.iterator();