Package org.bson

Examples of org.bson.BSONObject


   
    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"));
    Assert.assertEquals("name-updated-20", result.get("name"));
   
   
  }
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);
    }
   
View Full Code Here

   
    // 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());
View Full Code Here

    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

   
    Assert.assertEquals(100, response.getNumberReturned());
    Assert.assertEquals(100, response.getDocuments().size());
   
    for (int i = 0; i < 100; i++) {
      BSONObject doc = response.getDocuments().get(i);
      Assert.assertTrue(doc.containsField("_id"));
      Assert.assertTrue(doc.containsField("name"));
      Assert.assertFalse(doc.containsField("value"));
    }
  }
View Full Code Here

  @Test
  public void testCursor() throws Exception {
    MongoCursor cursor = client.cursor("test", "testQuery");
    int count = 0;
    while (cursor.hasNext()) {
      BSONObject doc = cursor.next();
      Assert.assertNotNull(doc);
      count++;
    }
    Assert.assertEquals(100, count);
   
    // batch size
    cursor = client.cursor("test", "testQuery").batchSize(10);
    count = 0;
    while (cursor.hasNext()) {
      BSONObject doc = cursor.next();
      Assert.assertNotNull(doc);
      count++;
    }
    Assert.assertEquals(100, count);
  }
View Full Code Here

    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"));
   
    // prepare data
    for (int i = 0; i < 100; i++) {
      BSONObject doc = createObject(i);
      client.insert(new Insert("test", "testQuery", doc));
    }
  }
View Full Code Here

      } catch (Exception e) {
      }
    }

    private void serveRequest() throws IOException {
      BSONObject reqHead = decoder.readObject(clientSocket.getInputStream());
      BSONObject reqBody = decoder.readObject(clientSocket.getInputStream());

      BSONObject respHead = new BasicBSONObject();
      respHead.put(Constants.SERVICE_METHOD,
          ((String) reqHead.get(Constants.SERVICE_METHOD)).getBytes());
      respHead.put(Constants.SEQ, (UnsignedLong) reqHead.get(Constants.SEQ));
      if (!"Arith.Multiply".equals((String) reqHead.get(Constants.SERVICE_METHOD))) {
        respHead
            .put(Constants.ERROR, ("rpc: can't find method " + (String) reqHead
                .get(Constants.SERVICE_METHOD)).getBytes());
      }

      BSONObject respBody = new BasicBSONObject();
      if (!respHead.containsField(Constants.ERROR)) {
        long A = 0;
        long B = 0;
        if (reqBody.containsField("A")) {
          A = (long) reqBody.get("A");
        }
        if (reqBody.containsField("B")) {
          B = (long) reqBody.get("B");
        }
        respBody.put(BsonClientCodec.MAGIC_TAG, A * B);
      }

      clientSocket.getOutputStream().write(encoder.encode(respHead));
      clientSocket.getOutputStream().write(encoder.encode(respBody));
    }
View Full Code Here

    Map<String, Long> args = new HashMap<>();
    args.put("A", 10L);
    args.put("Count", 20L);
    args.put("ErrorAt", -1L);
    args.put("BadTypeAt", -1L);
    BSONObject mArgs = new BasicBSONObject();
    mArgs.putAll(args);
    client.streamCall("Arith.Thrive", mArgs);
    Response response;

    long index = 0;
    while ((response = client.streamNext()) != null) {
      BSONObject result = (BSONObject) response.getReply();
      Assert.assertEquals(args.get("A"), result.get("C"));
      Assert.assertEquals(index, result.get("Index"));
      index++;
    }
    Assert.assertEquals(args.get("Count").longValue(), index);
    client.close();
  }
View Full Code Here

    Map<String, Long> args = new HashMap<>();
    args.put("A", 10L);
    args.put("Count", 20L);
    args.put("ErrorAt", 5L);
    args.put("BadTypeAt", -1L);
    BSONObject mArgs = new BasicBSONObject();
    mArgs.putAll(args);
    client.streamCall("Arith.Thrive", mArgs);
    Response response;
    long index = 0;
    while (index < args.get("ErrorAt") && (response = client.streamNext()) != null) {
      BSONObject result = (BSONObject) response.getReply();
      Assert.assertEquals(args.get("A"), result.get("C"));
      Assert.assertEquals(index, result.get("Index"));
      index++;
    }

    Assert.assertEquals(args.get("ErrorAt").longValue(), index);
    try {
View Full Code Here

TOP

Related Classes of org.bson.BSONObject

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.