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

费罗兹·西迪基(Feroz Siddiqui)

在此处输入图片说明我正在尝试实现一种方法,在该方法中,我将键的Hashmap创建为字符串,将值创建为对象。使用此哈希图,我需要搜索和排序Mongo中存在的数据集合。

下面是mongo db文件的id类:

public class Oid{
            String $oid;
            public String get$oid() {
                return $oid;
            }

            public void set$oid(String $oid) {
                this.$oid = $oid;
            }
        }

下面是pojo java文件:

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;
  }
 }

我正在使用gson转换此pojo,并在获取它的同时将其转换回java对象。

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", "[email protected]");
   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());
   }
  }
 }

在这个测试课程中,我希望获得两个记录的列表。第一个记录将包含电子邮件值“ [email protected]”,第二个记录的密码值为Quo Lux

s7vr

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

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();

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

MongoDb:如何在多个集合中搜索?

如何在MongoDB中按日期对集合进行排序?

在MongoDB中对集合进行递归搜索

需要对使用mongodb聚合查询从另一个集合连接的多个字段进行不同的计数

使用mongodb搜索多个集合

在MongoDB中的多个字段上使用$ in

mongoDB对多个字段进行排序

如何使用Java从MongoDB集合中检索特定字段的值

MongoDB如何按这样的多个字段排序?

使用MongoDB从单独集合中的文档聚合多个字段

如何为MongoDB集合中的文档选择单个字段?

在mongodb中搜索多个集合

查询,投影和排序mongoDB集合中文档的多个字段

如何使用多个本地字段比较对mongoDB集合进行链式聚合?

使用关联集合字段进行查找和排序的MongoDB聚合正在减慢查询速度

在mongodb中按日期和时间对集合进行排序

Mongodb搜索多个集合

如何使用Mongoose在MongoDB中搜索多个集合

在MongoDB中搜索期间如何忽略某个字段

MongoDB设置多个字段进行更新

MongoDB:如何考虑多个字段按距离排序?

如何在MongoDB中同时对多个集合进行全文搜索

如何使用Perl在mongodb中按多个字段排序?

如何使用mongoDB按数组中的多个值进行搜索?

使用MeteorJS在MongoDB中搜索多个字段

如何在MongoDB中按行对集合进行排序?

如何使用 MongoDB 中的 2 个字段的总和进行排序

如何使用multer从同一个mongodb集合的多个字段上传文件

如何在 MongoDB 中按带条件的多个字段进行排序