public SQLWarning getWarnings() throws SQLException {
   if (isClosed()) {
     throw new MongoSQLException("connection is closed");
   }
   String err = (String) db.getLastError().get("err");
   if (null != err) {
     return new SQLWarning(db.getLastError().toString());
   }
   return null;
 }
Exemple #2
0
  public String newDocument(InputStream in, String json) throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);

      GridFSInputFile gridFSInputFile = gridFS.createFile(in);
      ObjectId objectId = (ObjectId) gridFSInputFile.getId();
      String GUID = objectId.toStringMongod();

      DBObject dbObject = (DBObject) JSON.parse(json);

      gridFSInputFile.setFilename((String) dbObject.get(NAME));
      gridFSInputFile.setContentType((String) dbObject.get(CONTENT_TYPE));
      gridFSInputFile.setMetaData(dbObject);
      gridFSInputFile.save();
      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }

      return GUID;
    } catch (Exception e) {
      log.error("newDocument error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
Exemple #3
0
  public String newDocument(InputStream in, Map<String, String> properties)
      throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);

      GridFSInputFile gridFSInputFile = gridFS.createFile(in);
      ObjectId id = (ObjectId) gridFSInputFile.getId();
      String GUID = id.toStringMongod();

      gridFSInputFile.setFilename(properties.get(DocumentConnector.NAME));
      gridFSInputFile.setContentType(properties.get(DocumentConnector.CONTENT_TYPE));
      gridFSInputFile.put(DocumentConnector.ID, properties.get(DocumentConnector.ID));
      gridFSInputFile.put(
          DocumentConnector.DOCUMENT_TYPE, properties.get(DocumentConnector.DOCUMENT_TYPE));
      gridFSInputFile.save();
      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }

      return GUID;
    } catch (Exception e) {
      log.error("newDocument error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
Exemple #4
0
  public List<String> findDocuments(String id) throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);
      String key = "metadata." + DocumentConnector.ID;
      BasicDBObject query = new BasicDBObject(key, id);
      List<GridFSDBFile> gridFSDBFiles = gridFS.find(query);

      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }

      List<String> objects = new ArrayList<String>();
      for (GridFSDBFile gridFSDBFile : gridFSDBFiles) {
        ObjectId objectId = (ObjectId) gridFSDBFile.getId();
        objects.add(objectId.toStringMongod());
      }

      return objects;
    } catch (Exception e) {
      log.error("findDocuments error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
Exemple #5
0
  public String getDocument(String GUID, OutputStream out) throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);

      ObjectId key = new ObjectId(GUID);
      GridFSDBFile gridFSDBFile = gridFS.find(key);

      if (gridFSDBFile == null) {
        throw new DocumentException("No existe el documento");
      }
      if (out != null) gridFSDBFile.writeTo(out);
      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }

      DBObject dbObject = gridFSDBFile.getMetaData();
      return JSON.serialize(dbObject);
    } catch (Exception e) {
      log.error("getDocument error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
Exemple #6
0
 @Test
 public void testGetLastError() {
   Fongo fongo = newFongo();
   DB db = fongo.getDB("db");
   DBCollection collection = db.getCollection("coll");
   collection.insert(new BasicDBObject("_id", 1));
   CommandResult error = db.getLastError();
   assertTrue(error.ok());
 }
Exemple #7
0
  public void deleteDocument(String GUID) throws DocumentException {

    try {
      GridFS gridFS = new GridFS(dataBase);

      ObjectId key = new ObjectId(GUID);
      gridFS.remove(key);
      CommandResult result = dataBase.getLastError();
      if (!result.ok()) {
        throw new DocumentException(result.getErrorMessage());
      }
    } catch (Exception e) {
      log.error("deleteDocument error:" + e.getMessage());
      e.printStackTrace();
      throw new DocumentException(e.getMessage());
    }
  }
Exemple #8
0
  public static void main(String[] args) throws UnknownHostException {

    MongoClient mongo = new MongoClient("10.66.218.46", 27017);
    DB db = mongo.getDB("mydb");

    Set<String> collectionNames = db.getCollectionNames();
    for (String s : collectionNames) {
      System.out.println(s);
    }

    DBCollection conn = db.getCollection("testCollection");

    conn.drop();

    BasicDBObject doc =
        new BasicDBObject()
            .append("name", "MongoDB")
            .append("type", "database")
            .append("count", 1)
            .append("info", new BasicDBObject("x", 203).append("y", 102));

    conn.insert(doc);

    DBObject myDoc = conn.findOne();

    System.out.println(myDoc);

    for (int i = 0; i < 100; i++) {
      conn.insert(new BasicDBObject().append("i", i));
    }
    System.out.println(
        "total # of documents after inserting 100 small ones (should be 101) " + conn.getCount());

    // now use a query to get 1 document out
    DBCursor cursor = conn.find();
    try {
      while (cursor.hasNext()) {
        System.out.println(cursor.next());
      }
    } finally {
      cursor.close();
    }

    // now use a range query to get a larger subset
    BasicDBObject query = new BasicDBObject("i", 71);
    cursor = conn.find(query);

    try {
      while (cursor.hasNext()) {
        System.out.println(cursor.next());
      }
    } finally {
      cursor.close();
    }

    // range query with multiple constraints
    query =
        new BasicDBObject(
            "i", new BasicDBObject("$gt", 20).append("$lte", 30)); // i.e. 20 < i <= 30
    cursor = conn.find(query);
    try {
      while (cursor.hasNext()) {
        System.out.println(cursor.next());
      }
    } finally {
      cursor.close();
    }

    // now use a range query to get a larger subset
    query = new BasicDBObject("i", new BasicDBObject("$gt", 50)); // i.e. find all where i > 50
    cursor = conn.find(query);

    try {
      while (cursor.hasNext()) {
        System.out.println(cursor.next());
      }
    } finally {
      cursor.close();
    }

    // create an index on the "i" field
    conn.createIndex(new BasicDBObject("i", 1)); // create index on "i", ascending

    // list the indexes on the collection
    List<DBObject> list = conn.getIndexInfo();
    for (DBObject o : list) {
      System.out.println(o);
    }

    // See if the last operation had an error
    System.out.println("Last error : " + db.getLastError());

    // see if any previous operation had an error
    System.out.println("Previous error : " + db.getPreviousError());

    // force an error
    db.forceError();

    // See if the last operation had an error
    System.out.println("Last error : " + db.getLastError());

    db.resetError();

    mongo.close();
  }