示例#1
0
  private String selectBooks(
      String schema, String collection, String title, String author, String isbn)
      throws UnknownHostException {
    System.out.println(schema);
    System.out.println(collection);
    System.out.println(title);
    System.out.println(author);
    List<String> l = new ArrayList<String>();
    MongoDBConnectionManager mgr = MongoDBConnectionManager.getInstance();
    Mongo mongo = mgr.getMongo();
    DB db = mongo.getDB(schema);

    try {
      DBCollection c = db.getCollection(collection);
      BasicDBObject titleQuery = new BasicDBObject();
      titleQuery.put("title", new BasicDBObject("$regex", title));
      BasicDBObject authorQuery = new BasicDBObject();
      authorQuery.put("author", new BasicDBObject("$regex", author));

      BasicDBList or = new BasicDBList();
      or.add(titleQuery);
      or.add(titleQuery);

      DBObject query = new BasicDBObject("$and", or);

      DBCursor cursor = c.find(titleQuery);
      while (cursor.hasNext()) l.add(cursor.next().toString());
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
    System.out.println(l);
    if (l.size() > 0) return l.toString();
    else return null;
  }
示例#2
0
  private String selectBooks(String collectionType, String type, String text, String schema)
      throws UnknownHostException {
    System.out.println(type);
    System.out.println(schema);
    List<String> l = new ArrayList<String>();
    MongoDBConnectionManager mgr = MongoDBConnectionManager.getInstance();
    Mongo mongo = mgr.getMongo();
    DB db = mongo.getDB(schema);
    // collectionType = "view"
    try {
      DBCollection collection = db.getCollection(collectionType);
      BasicDBObject searchQuery = new BasicDBObject();
      BasicDBObject regexQuery = new BasicDBObject();
      // regexQuery.put("title", new BasicDBObject("$regex", "^.*mi.*$").append("$options", "i"));

      regexQuery.put(type, new BasicDBObject("$regex", text).append("$options", "i"));

      DBCursor cursor = collection.find(regexQuery);
      while (cursor.hasNext()) l.add(cursor.next().toString());
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
    System.out.println(l);
    if (l.size() > 0) return l.toString();
    return "error";
  }