Package org.bson

Examples of org.bson.BasicBSONObject


 
  @Test
  public void testUpdate() throws Exception {
   
    MongoCollection collection = client.getCollection("test", "client");
    collection.remove(new BasicBSONObject());
    Assert.assertEquals(0, collection.count());
   
    for (int i = 0; i < 100; i++) {
      BSONObject doc = new BasicBSONObject()
      .append("_id", i)
      .append("name", "name-" + i)
      .append("value", "value-" + i);
      collection.insert(doc);
    }
   
    Assert.assertEquals(100, collection.count());
   
    for (int i = 0; i < 100; i++) {
      collection.update(
          new BasicBSONObject("_id", i),
          new BasicBSONObject("$set", new BasicBSONObject("name", "updated-name-" + i)));
    }
   
    Assert.assertEquals(100, collection.count());
   
    int count = 0;
   
    for (BSONObject object : collection.cursor()) {
      int i = (Integer) object.get("_id");
      Assert.assertEquals("updated-name-" + i, object.get("name"));
      Assert.assertEquals("value-" + i, object.get("value"));
      count++;
    }
   
    Assert.assertEquals(100, count);
   
    for (int i = 0; i < 100; i++) {
      collection.update(
          new BasicBSONObject("_id", i),
          new BasicBSONObject("_id", i)
          .append("name", "new-name-" + i)
      );
    }

    count = 0;
   
    for (BSONObject object : collection.cursor()) {
      int i = (Integer) object.get("_id");
      Assert.assertEquals("new-name-" + i, object.get("name"));
      Assert.assertFalse(object.containsField("value"));
      count++;
    }
   
    Assert.assertEquals(100, count);
   
    for (int i = 50; i < 150; i++) {
      collection.upsert(
          new BasicBSONObject("_id", i),
          new BasicBSONObject("_id", i)
          .append("name", "upsert-name-" + i)
          .append("value", "upsert-value-" + i)
      );
    }
   
View Full Code Here


 
  @Test
  public void testRemove() throws Exception {
   
    MongoCollection collection = client.getCollection("test", "client");
    collection.remove(new BasicBSONObject());
    Assert.assertEquals(0, collection.count());
   
    for (int i = 0; i < 100; i++) {
      BSONObject doc = new BasicBSONObject()
      .append("_id", i)
      .append("name", "name-" + i)
      .append("value", "value-" + i);
      collection.insert(doc);
    }
   
    Assert.assertEquals(100, collection.count());
   
    for (int i = 0; i < 100; i++) {
      Assert.assertEquals(1, collection.count(new BasicBSONObject("_id", i)));
      collection.remove(new BasicBSONObject("_id", i));
      Assert.assertEquals(0, collection.count(new BasicBSONObject("_id", i)));
    }
   
    Assert.assertEquals(0, collection.count());
  }
View Full Code Here

   
    MongoCollection collection = client.getCollection("test", "client");
    collection.drop();
   
    for (int i = 0; i < 100; i++) {
      BSONObject doc = new BasicBSONObject()
      .append("_id", i)
      .append("name", "name-" + i);
      collection.insert(doc);
    }
   
   
    BSONObject result =
      collection.findAndModify()
      .query(new BasicBSONObject("_id", 20))
      .update(new BasicBSONObject("_id", 20).append("name", "name-updated-" + 20))
      .getnew()
      .execute();
   
    Assert.assertNotNull(result);
    Assert.assertEquals(20, result.get("_id"));
View Full Code Here

   
    MongoCollection indexCollection = client.getCollection("test", "system.indexes");
    long currentIndexCount = indexCollection.count();
   
    for (int i = 0; i < 100; i++) {
      BSONObject doc = new BasicBSONObject()
      .append("_id", i)
      .append("name", "name-" + i);
      collection.insert(doc);
    }
   
    Assert.assertEquals(100, collection.count());
   
    collection.createIndex("name_", new BasicBSONObject("name", 1), new BasicBSONObject());
   
    Assert.assertEquals(currentIndexCount+2, indexCollection.count());
   
   
  }
View Full Code Here

 
  @Test
  public void testQuery() throws Exception {
   
    // normal query
    Response response = client.query(new Query("test", "testQuery", 0, 100, new BasicBSONObject()));
    Assert.assertEquals(100, response.getNumberReturned());
    Assert.assertEquals(100, response.getDocuments().size());
    for (BSONObject doc : response.getDocuments()) {
      int id = ((Number) doc.get("_id")).intValue();
      Assert.assertNotNull(doc);
      Assert.assertEquals(id, doc.get("_id"));
      Assert.assertEquals("name-" + id, doc.get("name"));
      Assert.assertEquals("value-" + (id*100), doc.get("value"));
    }
   
    List<BSONObject> first10 = response.getDocuments().subList(0, 10);
    List<BSONObject> next10 = response.getDocuments().subList(10, 20);
   
    // limited query
    response = client.query(new Query("test", "testQuery", 0, 10, new BasicBSONObject()));
    Assert.assertEquals(10, response.getNumberReturned());
    Assert.assertEquals(10, response.getDocuments().size());
    Assert.assertEquals(first10, response.getDocuments());
   
    // skipped query
    response = client.query(new Query("test", "testQuery", 10, 10, new BasicBSONObject()));
    Assert.assertEquals(10, response.getNumberReturned());
    Assert.assertEquals(10, response.getDocuments().size());
    Assert.assertEquals(next10, response.getDocuments());
   
    // over skipped query
    response = client.query(new Query("test", "testQuery", 10, 100, new BasicBSONObject()));
    Assert.assertEquals(90, response.getNumberReturned());
    Assert.assertEquals(90, response.getDocuments().size());
   
    // equals query
    response = client.query(new Query("test", "testQuery", 0, 1, new BasicBSONObject("_id", 50)));
    Assert.assertEquals(1, response.getNumberReturned());
    Assert.assertEquals(1, response.getDocuments().size());
    BSONObject doc = response.getDocuments().get(0);
    Assert.assertEquals(50, doc.get("_id"));
   
    // greater than query
    response = client.query(new Query("test", "testQuery", 0, 100,
        new BasicBSONObject("_id", new BasicBSONObject("$gt", 50))));
    Assert.assertEquals(49, response.getNumberReturned());
    Assert.assertEquals(49, response.getDocuments().size());
    response = client.query(new Query("test", "testQuery", 0, 100,
        new BasicBSONObject("_id", new BasicBSONObject("$gte", 50))));
    Assert.assertEquals(50, response.getNumberReturned());
    Assert.assertEquals(50, response.getDocuments().size());
 
    // lesser than query
    response = client.query(new Query("test", "testQuery", 0, 100,
        new BasicBSONObject("_id", new BasicBSONObject("$lt", 50))));
    Assert.assertEquals(50, response.getNumberReturned());
    Assert.assertEquals(50, response.getDocuments().size());
    response = client.query(new Query("test", "testQuery", 0, 100,
        new BasicBSONObject("_id", new BasicBSONObject("$lte", 50))));
    Assert.assertEquals(51, response.getNumberReturned());
    Assert.assertEquals(51, response.getDocuments().size());
   
    // combined query
    response = client.query(new Query("test", "testQuery", 0, 100,
        new BasicBSONObject("_id", new BasicBSONObject("$gt", 20).append("$lt", 80))));
    Assert.assertEquals(59, response.getNumberReturned());
    Assert.assertEquals(59, response.getDocuments().size());
   
  }
View Full Code Here

  @Test
  public void testSort() throws Exception {
   
    // order integer
    Response response = client.query(new Query("test", "testQuery", 0, 100,
        new BasicBSONObject("$query",new BasicBSONObject())
          .append("$orderby", new BasicBSONObject("_id", 1))));
    Assert.assertEquals(100, response.getNumberReturned());
    for (int i = 0; i < 100; i++) {
      BSONObject doc = response.getDocuments().get(i);
      Assert.assertEquals(i, doc.get("_id"));
    }
   
    // order integer (reversed)
    response = client.query(new Query("test", "testQuery", 0, 100,
        new BasicBSONObject("$query", new BasicBSONObject())
        .append("$orderby", new BasicBSONObject("_id", -1))));
    Assert.assertEquals(100, response.getNumberReturned());
    for (int i = 0; i < 100; i++) {
      BSONObject doc = response.getDocuments().get(i);
      Assert.assertEquals(99-i, doc.get("_id"));
    }
View Full Code Here

 
  @Test
  public void testReturnFields() throws Exception {
   
    Response response = client.query(new Query(
        "test", "testQuery", 0, 100, new BasicBSONObject(), new BasicBSONObject("name", 1)
    ));
   
    Assert.assertEquals(100, response.getNumberReturned());
    Assert.assertEquals(100, response.getDocuments().size());
   
View Full Code Here

  /**
   * データ準備
   */
  private void prepareData() {
   
    Response response = client.query(new Query("test", "testQuery", 0, 100, new BasicBSONObject(), null));
    client.delete(new Delete("test", "testQuery", new BasicBSONObject()));
    response = client.query(new Query("test", "$cmd", 0, 1, new BasicBSONObject("count", "testQuery"), null));
   
    Assert.assertEquals(1, response.getNumberReturned());
    BSONObject result = response.getDocuments().get(0);
    Assert.assertEquals(0.0, result.get("n"));
   
View Full Code Here

      client.insert(new Insert("test", "testQuery", doc));
    }
  }
 
  private BSONObject createObject(int number) {
    return new BasicBSONObject("_id", number)
      .append("name", "name-" + number)
      .append("value", "value-" + (number * 100));
  }
 
View Full Code Here

        if (!query.containsField("remove") && !query.containsField("update")) {
            throw new MongoServerException("need remove or update");
        }

        BSONObject queryObject = new BasicBSONObject();

        if (query.containsField("query")) {
            queryObject.put("query", query.get("query"));
        } else {
            queryObject.put("query", new BasicBSONObject());
        }

        if (query.containsField("sort")) {
            queryObject.put("orderby", query.get("sort"));
        }

        BSONObject lastErrorObject = null;
        BSONObject returnDocument = null;
        int num = 0;
        for (BSONObject document : handleQuery(queryObject, 0, 1)) {
            num++;
            if (Utils.isTrue(query.get("remove"))) {
                removeDocument(document);
                returnDocument = document;
            } else if (query.get("update") != null) {
                BSONObject updateQuery = (BSONObject) query.get("update");

                Integer matchPos = matcher.matchPosition(document, (BSONObject) queryObject.get("query"));

                BSONObject oldDocument = updateDocument(document, updateQuery, matchPos);
                if (returnNew) {
                    returnDocument = document;
                } else {
                    returnDocument = oldDocument;
                }
                lastErrorObject = new BasicBSONObject("updatedExisting", Boolean.TRUE);
                lastErrorObject.put("n", Integer.valueOf(1));
            }
        }
        if (num == 0 && Utils.isTrue(query.get("upsert"))) {
            BSONObject selector = (BSONObject) query.get("query");
            BSONObject updateQuery = (BSONObject) query.get("update");
            BSONObject newDocument = handleUpsert(updateQuery, selector);
            if (returnNew) {
                returnDocument = newDocument;
            } else {
                returnDocument = new BasicBSONObject();
            }
            num++;
        }

        if (query.get("fields") != null) {
            BSONObject fields = (BSONObject) query.get("fields");
            returnDocument = projectDocument(returnDocument, fields);
        }

        BSONObject result = new BasicBSONObject();
        if (lastErrorObject != null) {
            result.put("lastErrorObject", lastErrorObject);
        }
        result.put("value", returnDocument);
        Utils.markOkay(result);
        return result;
    }
View Full Code Here

TOP

Related Classes of org.bson.BasicBSONObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.