Exemple #1
0
 @Test
 public void testUpdateWithIdIn() {
   DBCollection collection = newCollection();
   collection.insert(new BasicDBObject("_id", 1));
   DBObject query =
       new BasicDBObjectBuilder().push("_id").append("$in", Arrays.asList(1)).pop().get();
   DBObject update =
       new BasicDBObjectBuilder()
           .push("$push")
           .push("n")
           .append("_id", 2)
           .append("u", 3)
           .pop()
           .pop()
           .push("$inc")
           .append("c", 4)
           .pop()
           .get();
   DBObject expected =
       new BasicDBObjectBuilder()
           .append("_id", 1)
           .append("n", Arrays.asList(new BasicDBObject("_id", 2).append("u", 3)))
           .append("c", 4)
           .get();
   collection.update(query, update, false, true);
   assertEquals(expected, collection.findOne());
 }
  public void LoadSeedData(String strColl, String metadatafile) throws Exception {
    try {
      InputStream is = this.getClass().getResourceAsStream(metadatafile);
      String xml;
      xml = IOUtils.toString(is);
      JSONObject rwSeed = XML.toJSONObject(xml);

      if (!rwSeed.isNull("ReferralWireSeedData")) {
        JSONObject json = rwSeed.getJSONObject("ReferralWireSeedData");
        DBCollection dbcoll = store.getColl(strColl);

        Object coll = (Object) json.get(strColl);
        if (coll instanceof JSONArray) {
          JSONArray defs = (JSONArray) coll;
          for (int i = 0; i < defs.length(); i++) {
            Object obj = (JSONObject) defs.get(i);
            dbcoll.insert((DBObject) com.mongodb.util.JSON.parse(obj.toString()));
          }
        } else {
          dbcoll.insert((DBObject) com.mongodb.util.JSON.parse(coll.toString()));
        }
      }
    } catch (IOException e) {
      // TODO Auto-generated catch block
      log.debug("API Error: ", e);
    }
  }
  public JsonArray addPerfResults(String perfResults) {
    DBCollection coll = db.getCollection("performance");
    BasicDBObject newPerfResults = (BasicDBObject) JSON.parse(perfResults);
    newPerfResults.append("_id", newPerfResults.getString("name"));

    BasicDBObject query = new BasicDBObject();
    query.append("name", newPerfResults.getString("name"));

    BasicDBObject removeId = new BasicDBObject("_id", 0);
    DBCursor cursor = coll.find(query, removeId);

    JsonObjectBuilder objBuild = Json.createObjectBuilder();
    JsonArrayBuilder arrBuild = Json.createArrayBuilder();
    JsonObject json = objBuild.build();

    if (cursor.count() > 0) {
      LOG.info("Performance Results Found: ");
      BasicDBObject found = (BasicDBObject) cursor.next();
      json = Json.createReader(new StringReader(found.toString())).readObject();
      arrBuild.add(json);
    } else {
      LOG.info("New Performance Results Created: " + coll.insert(newPerfResults));
      json = Json.createReader(new StringReader(newPerfResults.toString())).readObject();
      arrBuild.add(json);
    }

    LOG.info(
        "----------------------------------- ARR BUILD -------------------------------------------------\n"
            + arrBuild.build().toString());

    return arrBuild.build();
  }
Exemple #4
0
 /**
  * 上午11:27:09
  *
  * @author zhangyh2 DBMongo.java TODO 查找全部地址信息
  * @return
  */
 public static List<AddressModel> selectAllAddress(String token) {
   // TODO Auto-generated method stub
   DBCollection collection = DBMongo.getInstance().getDB().getCollection(tablename);
   DBCursor dbObject = collection.find(new BasicDBObject("token", token));
   try {
     // 增删查改必须放到线程中进行。否则报错。
     List<AddressModel> data = new ArrayList<AddressModel>();
     while (dbObject.hasNext()) {
       DBObject ob = dbObject.next();
       data.add(
           new AddressModel(
               ob.get("_id").toString(),
               ob.get("name").toString(),
               ob.get("tel").toString(),
               ob.get("province").toString(),
               ob.get("city").toString(),
               ob.get("district").toString(),
               ob.get("zipcode").toString(),
               ob.get("token").toString()));
     }
     return data;
   } catch (Exception e) {
     // TODO: handle exception
   } finally {
     if (dbObject != null) {
       dbObject.close();
     }
   }
   return null;
 }
Exemple #5
0
  @Override
  public String IngresarJson(String nombreDB, String json, int clienteId)
      throws UnknownHostException {
    // TODO Auto-generated method stub
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB base = mongoClient.getDB(nombreDB);

    // Ya existe
    if (ExisteCliente(nombreDB, clienteId)) {
      //        	System.out.println("********************\n");
      //        	System.out.println("Ya existe cliente, error no se igresa el Json");
      //        	System.out.println("********************\n");
      return "Ya existe cliente, error no se igresa el Json";
    }

    // no existe el cliente
    DBCollection collection = base.getCollection("Json");
    BasicDBObject document = new BasicDBObject();
    document.put("id", clienteId);

    DBObject object = (DBObject) JSON.parse(json);
    document.put("json", object);
    // document.put("json",json);
    collection.insert(document);
    return "Ok";
  }
 @Before
 public void init() throws IOException {
   action = new MethodMsgAction();
   MapPubSub pubSub = new MapPubSub();
   DB db = initMongoDB();
   collection = db.getCollection("customer");
   collection.drop();
   BasicDBObject basicDBObject = new BasicDBObject();
   basicDBObject.put("_id", "testid");
   basicDBObject.put("testField", "pippo");
   collection.insert(basicDBObject);
   action.addInvoker(
       new MongoDBMethodInvoker(
           db,
           pubSub,
           new IdGenerator() {
             @Override
             public String generateCollectionID(String randomSeed) {
               return "fixed";
             }
           }));
   mockSession = new MockSession();
   session = new DDPSession(mockSession);
   pubSub.sub(
       new Sub() {
         {
           setName("customer");
         }
       },
       session);
 }
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    // TODO Auto-generated method stub
    // response.getWriter().append("Served at: ").append(request.getContextPath());
    MongoClientURI uri =
        new MongoClientURI("mongodb://*****:*****@ds019028.mlab.com:19028/asedb");
    MongoClient client = new MongoClient(uri);
    DB db = client.getDB(uri.getDatabase());
    DBCollection users = db.getCollection("UserRecords");
    BasicDBObject query = new BasicDBObject();

    String firstname = request.getParameter("FirstName");
    String lastname = request.getParameter("LastName");
    String email = request.getParameter("email");
    String password = request.getParameter("EnterPassword");
    String confpasswd = request.getParameter("ConfirmPassword");
    query.put("First Name", firstname);
    query.put("Last Name", lastname);
    query.put("Email", email);
    System.out.println(email);
    if (password == confpasswd) {
      query.put("Password", password);
    } else {

    }
    DBCursor docs = users.find(query);
    response.getWriter().write(docs.toArray().toString());

    response.setHeader("Access-Control-Allow-Origin", "*");
    response.setHeader("Access-Control-Allow-Methods", "GET");
    response.setHeader("Access-Control-Allow-Headers", "Content-Type");
    response.setHeader("Access-Control-Max-Age", "86400");
    System.out.println("Insert doget");
  }
Exemple #8
0
 @Test
 public void testDropDatabaseAlsoDropsCollectionData() throws Exception {
   DBCollection collection = newCollection();
   collection.insert(new BasicDBObject());
   collection.getDB().dropDatabase();
   assertEquals("Collection should have no data", 0, collection.count());
 }
  /*------------------------------------------------------------ */
  @Override
  protected void invalidateSession(String idInCluster) {
    __log.debug("MongoSessionManager:invalidateSession:invalidating " + idInCluster);

    super.invalidateSession(idInCluster);

    /*
     * pull back the 'valid' value, we can check if its false, if is we don't need to
     * reset it to false
     */
    DBObject validKey = new BasicDBObject(__VALID, true);
    DBObject o = _sessions.findOne(new BasicDBObject(__ID, idInCluster), validKey);

    if (o != null && (Boolean) o.get(__VALID)) {
      BasicDBObject update = new BasicDBObject();
      BasicDBObject sets = new BasicDBObject();
      sets.put(__VALID, false);
      sets.put(__INVALIDATED, System.currentTimeMillis());
      update.put("$set", sets);

      BasicDBObject key = new BasicDBObject(__ID, idInCluster);

      _sessions.update(key, update);
    }
  }
Exemple #10
0
 @Test
 public void testSave() {
   DBCollection collection = newCollection();
   BasicDBObject inserted = new BasicDBObject("_id", 1);
   collection.insert(inserted);
   collection.save(inserted);
 }
Exemple #11
0
 @Test
 public void testInsertDuplicateIgnored() {
   DBCollection collection = newCollection();
   collection.insert(new BasicDBObject("_id", 1));
   collection.insert(new BasicDBObject("_id", 1));
   assertEquals(1, collection.count());
 }
Exemple #12
0
 @Test
 public void testConvertJavaMapToDBObject() {
   DBCollection collection = newCollection();
   collection.insert(new BasicDBObject("_id", 1).append("n", Collections.singletonMap("a", 1)));
   DBObject result = collection.findOne();
   assertTrue("not a DBObject", result.get("n") instanceof BasicDBObject);
 }
Exemple #13
0
 @Test
 public void testConvertJavaListToDbList() {
   DBCollection collection = newCollection();
   collection.insert(new BasicDBObject("_id", 1).append("n", Arrays.asList(1, 2)));
   DBObject result = collection.findOne();
   assertTrue("not a DBList", result.get("n") instanceof BasicDBList);
 }
Exemple #14
0
  @Test
  public void testAnotherUpsert() {
    DBCollection collection = newCollection();
    BasicDBObjectBuilder queryBuilder =
        BasicDBObjectBuilder.start()
            .push("_id")
            .append("f", "ca")
            .push("1")
            .append("l", 2)
            .pop()
            .push("t")
            .append("t", 11)
            .pop()
            .pop();
    DBObject query = queryBuilder.get();

    DBObject update =
        BasicDBObjectBuilder.start()
            .push("$inc")
            .append("n.!", 1)
            .append("n.a.b:false", 1)
            .pop()
            .get();
    collection.update(query, update, true, false);

    DBObject expected =
        queryBuilder.push("n").append("!", 1).push("a").append("b:false", 1).pop().pop().get();
    assertEquals(expected, collection.findOne());
  }
  private void getTweetWithRTCountGreaterThan(int gt) {

    Mongo mongo = null;
    try {
      mongo = new Mongo(MONGO_DB_HOST, MONGO_DB_PORT);
      DB db = mongo.getDB(MONGO_DB_NAME);
      DBCollection collection = db.getCollection(MONGO_DB_COLLECTION);

      DBCursor cursor = collection.find(new BasicDBObject("rTCount", new BasicDBObject("$gt", gt)));

      DBObject dbObject;

      System.out.println("\n\n========================================");
      System.out.println("Displaying Tweet with RTCount > " + gt);

      while (cursor.hasNext()) {
        dbObject = cursor.next();
        System.out.println(
            "user "
                + dbObject.get("user")
                + "tweet "
                + dbObject.get("tweet")
                + " RTCount "
                + dbObject.get("rTCount"));
      }

      System.out.println("========================================\n\n");

    } catch (UnknownHostException e) {
      e.printStackTrace();
    } finally {
      if (mongo != null) mongo.close();
    }
  }
  /**
   * 获取表"collectionName"的主键id
   *
   * @param collectionName
   * @return
   */
  public Long getIndexIdFromMongo(String collectionName) {

    long indexNum = 0;
    try {
      // 主键存储表名:index
      DBCollection collection = getCollection("TableName.INDEX_TABLE_NAME");
      // 获取某个应用对象存储表的表名
      BasicDBObject oldDB = new BasicDBObject("name", collectionName);
      BasicDBObject update = new BasicDBObject("$inc", new BasicDBObject("id", 1));
      DBObject reFields = new BasicDBObject();
      reFields.put("id", "id");

      // 自增主键id
      DBObject result = collection.findAndModify(oldDB, reFields, null, false, update, true, true);
      if (result.get("id") == null) {
        throw new RuntimeException("获取主键id异常,请重试!!!");
      }
      indexNum = Long.parseLong(result.get("id").toString());
      if (indexNum <= 0) {
        throw new RuntimeException("获取主键id异常,请重试!!!");
      }
    } catch (Exception e) {
      logger.error("BaseMongoDAO.countRecord查询数据条数时发生异常,入参collectionName=" + collectionName, e);
    }
    return indexNum;
  }
  public static void main(String args[]) throws IOException {
    DBCollection dbCollection = new MongoResource().getCollection();

    ProductReader productReader = new ProductReader();
    BasicDBObject product;
    System.out.println("Inserting products ...");
    List<DBObject> products = new ArrayList<DBObject>();
    while ((product = productReader.read()) != null) {
      products.add(product);
    }
    dbCollection.insert(products);
    System.out.println("Finished.");

    MetaDataReader metaDataReader = new MetaDataReader();
    BasicDBObject metadata;

    System.out.println("Creating index on itemNumber");
    dbCollection.ensureIndex("itemNumber");
    System.out.println("Finished creating index on itemNumber");

    System.out.println("Updating products ...");
    while ((metadata = metaDataReader.read()) != null) {
      BasicDBObject existingProduct = new BasicDBObject("itemNumber", metadata.get("itemNumber"));
      dbCollection.update(existingProduct, metadata, true, false);
    }
    System.out.println("Finished.");
  }
  /*
   * (non-Javadoc)
   *
   * @see org.ow2.play.service.registry.api.Registry#put(java.lang.String,
   * java.lang.String)
   */
  @Override
  @WebMethod
  public void put(String name, String url) throws RegistryException {
    if (logger.isLoggable(Level.FINE)) {
      logger.fine(String.format("Put url %s for name %s", url, name));
    }
    checkInitialized();

    if (name == null || url == null) {
      throw new RegistryException("Can not put null values name = %s, url = %s", name, url);
    }

    // update the entry if it already exists
    DBObject filter = new BasicDBObject();
    filter.put(NAME_KEY, name);

    DBObject filtered = collection.findOne(filter);
    if (filtered != null) {
      filtered.put(URL_KEY, url);
      collection.save(filtered);
    } else {
      DBObject o = new BasicDBObject();
      o.put(NAME_KEY, name);
      o.put(URL_KEY, url);
      collection.insert(o);
    }
  }
  public void insertar(UsuarioOD Usuario) {

    DBCollection coleccionUsuario = conectarMongo();

    if (coleccionUsuario != null) {

      BasicDBObject usuario = new BasicDBObject();
      usuario.put("id_u", crearId());
      usuario.put("nombre", Usuario.getNombre());
      usuario.put("nombres", Usuario.getNombres());
      usuario.put("apellido", Usuario.getApellido());
      usuario.put("apellidos", Usuario.getApellidos());
      usuario.put("email", Usuario.getEmail());
      usuario.put("fecha", Usuario.getFecha());
      usuario.put("nick", Usuario.getNick());
      usuario.put("pais", Usuario.getPais());
      usuario.put("biografia", Usuario.getBiografia());
      usuario.put("sexo", Usuario.getSexo());
      usuario.put("clave", Usuario.getClave());
      coleccionUsuario.insert(usuario);

    } else {
      System.out.println("coleccion no existente");
    }
  }
  @Test
  public void testStore() {
    HasExpiryField hasExpiryField = new HasExpiryField();
    hasExpiryField.setOfferIs("Good");
    Calendar c = Calendar.getInstance();
    hasExpiryField.setOfferExpiresAt(c.getTime());
    ds.getMapper().addMappedClass(HasExpiryField.class);
    ds.ensureIndexes();
    ds.save(hasExpiryField);

    DB db = ds.getDB();
    DBCollection dbCollection = db.getCollection("HasExpiryField");
    List<DBObject> indexes = dbCollection.getIndexInfo();

    Assert.assertNotNull(indexes);
    Assert.assertEquals(2, indexes.size());
    DBObject index = null;
    for (DBObject candidateIndex : indexes) {
      if (candidateIndex.containsField("expireAfterSeconds")) {
        index = candidateIndex;
      }
    }
    Assert.assertNotNull(index);
    Assert.assertTrue(index.containsField("expireAfterSeconds"));
    Assert.assertEquals(60, index.get("expireAfterSeconds"));
  }
 @Override
 public DBObject findDocumentByObjectId(String collectionName, String objectId) {
   DBCollection dbCollection = db.getCollection(collectionName);
   BasicDBObject query = new BasicDBObject();
   query.put("_id", new ObjectId(objectId));
   return dbCollection.findOne(query);
 }
  @Override
  public void update(KeycloakSession session) throws ClassNotFoundException {
    DBCollection realmsCollection = db.getCollection("realms");
    realmsCollection.ensureIndex(new BasicDBObject("name", 1), new BasicDBObject("unique", true));

    DefaultMongoUpdaterProvider.log.debugv("Created collection {0}", "realms");

    createCollection("users");
    ensureIndex("users", new String[] {"realmId", "username"}, true, false);
    ensureIndex("users", "emailIndex", true, true);

    createCollection("roles");
    ensureIndex("roles", "nameIndex", true, false);

    createCollection("applications");
    ensureIndex("applications", new String[] {"realmId", "name"}, true, false);

    createCollection("oauthClients");
    ensureIndex("oauthClients", new String[] {"realmId", "name"}, true, false);

    createCollection("userFailures");

    createCollection("sessions");

    createCollection("clientSessions");
  }
Exemple #23
0
  @Override
  public String ActualizarJson(String nombreDB, String json, int clienteId)
      throws UnknownHostException {
    // TODO Auto-generated method stub

    if (!ExisteCliente(nombreDB, clienteId)) {
      //        	System.out.println("********************\n");
      //        	System.out.println("No existe el cliente, no se puede actualizar");
      //        	System.out.println("********************\n");
      return "No existe el cliente, no se puede actualizar";
    }
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB base = mongoClient.getDB(nombreDB);
    DBCollection collection = base.getCollection("Json");

    BasicDBObject document = new BasicDBObject();
    DBObject object = (DBObject) JSON.parse(json);
    document.put("id", clienteId);
    document.put("json", object);

    BasicDBObject query = new BasicDBObject().append("id", clienteId);

    collection.findAndModify(query, document);
    return "Cliente actualizado";
  }
  /**
   * All headers except collection and database are non available for this operation.
   *
   * @param exchange
   * @throws Exception
   */
  protected void doAggregate(Exchange exchange) throws Exception {
    DBCollection dbCol = calculateCollection(exchange);
    DBObject query = exchange.getIn().getMandatoryBody(DBObject.class);

    // Impossible with java driver to get the batch size and number to skip
    Iterable<DBObject> dbIterator = null;
    AggregationOutput aggregationResult = null;

    // Allow body to be a pipeline
    // @see http://docs.mongodb.org/manual/core/aggregation/
    if (query instanceof BasicDBList) {
      BasicDBList queryList = (BasicDBList) query;
      aggregationResult =
          dbCol.aggregate(
              (DBObject) queryList.get(0),
              queryList
                  .subList(1, queryList.size())
                  .toArray(new BasicDBObject[queryList.size() - 1]));
    } else {
      aggregationResult = dbCol.aggregate(query);
    }

    dbIterator = aggregationResult.results();
    Message resultMessage = prepareResponseMessage(exchange, MongoDbOperation.aggregate);
    resultMessage.setBody(dbIterator);
  }
Exemple #25
0
  @Override
  public String Json(String nombreDB, int clienteId) throws UnknownHostException, JSONException {
    // TODO Auto-generated method stub
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB base = mongoClient.getDB(nombreDB);

    DBCollection collection = base.getCollection("Json");
    BasicDBObject query = new BasicDBObject();
    query.put("id", clienteId);
    DBCursor cursor = collection.find(query);

    if (cursor.size() == 0) {
      //        	System.out.println("********************\n");
      //        	System.out.println("No existe el cliente, no se puede ingresar json");
      //        	System.out.println("********************\n");

      return "No existe el cliente, no se puede ingresar json";
    }
    // Existe el cliente

    DBObject objeto = (DBObject) cursor.next();

    DBObject json = (DBObject) objeto.get("json");
    //  DBObject j = JSON.parse(json.toString());
    JSONObject aj = new JSONObject(json.toString());

    return aj.toString();
  }
  private OperationResult optimisticCheckEtag(
      DBCollection coll,
      DBObject oldDocument,
      ObjectId newEtag,
      ObjectId requestEtag,
      int httpStatusIfOk) {
    Object oldEtag = oldDocument.get("_etag");

    if (oldEtag == null) { // well we don't had an etag there so fine
      return new OperationResult(httpStatusIfOk);
    }

    if (!(oldEtag instanceof ObjectId)) { // well the _etag is not an ObjectId. no check is possible
      return new OperationResult(httpStatusIfOk);
    }

    if (requestEtag == null) {
      coll.save(oldDocument);
      return new OperationResult(HttpStatus.SC_CONFLICT, oldEtag);
    }

    if (oldEtag.equals(requestEtag)) {
      return new OperationResult(httpStatusIfOk, newEtag);
    } else {
      // oopps, we need to restore old document
      // they call it optimistic lock strategy
      coll.save(oldDocument);
      return new OperationResult(HttpStatus.SC_PRECONDITION_FAILED, oldEtag);
    }
  }
  public JsonArray getPerfResults(String perfResults) {
    DBCollection coll = db.getCollection("performance");
    BasicDBObject query = new BasicDBObject();
    BasicDBObject removeId = new BasicDBObject("_id", 0);

    query.put("name", perfResults);

    DBCursor cursor = coll.find(query, removeId);
    JsonObjectBuilder objBuild = Json.createObjectBuilder();
    JsonArrayBuilder arrBuild = Json.createArrayBuilder();
    JsonObject json = objBuild.build();

    while (cursor.hasNext()) {
      BasicDBObject found = (BasicDBObject) cursor.next();
      LOG.info("Found Performance Results: " + found.toString());
      json = Json.createReader(new StringReader(found.toString())).readObject();

      arrBuild.add(json);
    }

    LOG.info(
        "----------------------------------- ARR BUILD -------------------------------------------------\n"
            + arrBuild.build().toString());

    return arrBuild.build();
  }
  @Test
  public void flushSavesObject() throws Exception {
    writer.flush();

    final DBCollection instances = testDb.getCollection(SPEC_NAME);
    assertEquals(1, instances.getCount());
  }
  public JsonArray updatePerfResults(String perfResults) {
    BasicDBObject doc = (BasicDBObject) JSON.parse(perfResults);
    doc.append("_id", doc.get("name"));

    DBCollection coll = db.getCollection("performance");
    BasicDBObject query = new BasicDBObject();

    query.put("_id", doc.get("name"));

    DBCursor cursor = coll.find(query);

    JsonObjectBuilder objBuild = Json.createObjectBuilder();
    JsonArrayBuilder arrBuild = Json.createArrayBuilder();
    JsonObject json = objBuild.build();

    if (cursor.count() > 0) {
      LOG.info("Performance Results Found and Updated: " + coll.update(query, doc, true, false));
      json = Json.createReader(new StringReader(perfResults)).readObject();
      return arrBuild.add(json).build();
    } else {
      LOG.info("Performance Results Created: " + coll.insert(doc));
      json = Json.createReader(new StringReader(perfResults)).readObject();
      return arrBuild.add(json).build();
    }
  }
Exemple #30
0
  @Override
  public void run() {
    MongoProfile profile = options.getProfile();

    MongoClient mongo = null;
    DBCursor cursor = null;
    try {
      mongo = new MongoClient(profile.getAddress(), profile.getCredentials());

      DB db = mongo.getDB(options.getDatabase());
      DBCollection col = db.getCollection(options.getCollection());
      cursor = col.find();

      while (cursor.hasNext()) {
        DBObject doc = cursor.next();

        Map<String, Object> m = convert(doc);
        pushPipe(new Row(m));
      }
    } catch (Throwable t) {
      slog.error("araqne logdb mongo: cannot run mongo.find", t);
    } finally {
      if (cursor != null) cursor.close();

      if (mongo != null) mongo.close();
    }
  }