com.mongodb.BasicDBObject.getObjectId()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(12.6k)|赞(0)|评价(0)|浏览(92)

本文整理了Java中com.mongodb.BasicDBObject.getObjectId()方法的一些代码示例,展示了BasicDBObject.getObjectId()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。BasicDBObject.getObjectId()方法的具体详情如下:
包路径:com.mongodb.BasicDBObject
类名称:BasicDBObject
方法名:getObjectId

BasicDBObject.getObjectId介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

DBCursor cursor = collection.find();
 while (cursor.hasNext()) {
   BasicDBObject current_object = (BasicDBObject) cursor.next();
   current_object.update(
       new BasicDBObject().append("_id", current_object.getObjectId("_id"), 
           new BasicDBObject()
               .append(
                   "$set", 
                   new BasicDBObject("employeeID", Integer.parseInt(current_object.getString("employeeID"))
                   )
               )
       )
   );
 }

代码示例来源:origin: gaillard/mongo-queue-java

final BasicDBObject message = (BasicDBObject) collection.findAndModify(builtQuery, fields, sort, false, update, true, false);
if (message != null) {
  final ObjectId id = message.getObjectId("_id");
  return ((BasicDBObject) message.get("payload")).append("id", id);

代码示例来源:origin: liveoak-io/liveoak

@Test
public void deleteGrandchildObject() throws Exception {
  String methodName = "testDeleteGrandchildObject";
  assertThat(db.getCollectionNames().contains(methodName)).isFalse();
  // create the object using the mongo driver directly
  BasicDBObject object = new BasicDBObject();
  object.append("foo", new BasicDBObject("bar", new BasicDBObject("ABC", new BasicDBObject("123", "XYZ"))));
  db.getCollection(methodName).insert(object);
  String id = object.getObjectId("_id").toString();
  assertThat(db.getCollection(methodName).findOne(new BasicDBObject("_id", new ObjectId(id)))).isNotNull();
  // we should not be able to directly delete a child object
  try {
    client.delete(new RequestContext.Builder().returnFields(new DefaultReturnFields("bar(ABC(*))")).build(), "/testApp/" + BASEPATH + "/" + methodName
        + "/" + id + "/foo");
    Fail.fail();
  } catch (ResourceNotFoundException e) {
    // expected
  }
  assertThat((DBObject) object).isEqualTo(db.getCollection(methodName).findOne());
}

代码示例来源:origin: liveoak-io/liveoak

@Test
public void getChildDirectly() throws Exception {
  String methodName = "testGetChildDirectly";
  assertThat(db.collectionExists(methodName)).isFalse();
  // create the object using the mongo driver directly
  BasicDBObject object = new BasicDBObject();
  object.append("foo", "bar").append("abc", "123").append("obj", new BasicDBObject("foo2", "bar2"));
  object.append("child", new BasicDBObject("grandchild", new BasicDBObject("foo3", "bar3")));
  db.getCollection(methodName).insert(object);
  assertEquals(1, db.getCollection(methodName).getCount());
  String id = "ObjectId(\"" + object.getObjectId("_id").toString() + "\")";
  try {
    client.read(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id + "/child");
    Fail.fail();
  } catch (ResourceNotFoundException e) {
    // expected
  }
}

代码示例来源:origin: liveoak-io/liveoak

@Test
public void deleteChildProperty() throws Exception {
  String methodName = "testDeleteChildProperty";
  assertThat(db.getCollectionNames().contains(methodName)).isFalse();
  // create the object using the mongo driver directly
  BasicDBObject object = new BasicDBObject();
  object.append("foo", new BasicDBObject("bar", new BasicDBObject("ABC", 123)));
  db.getCollection(methodName).insert(object);
  String id = object.getObjectId("_id").toString();
  assertThat(db.getCollection(methodName).findOne(new BasicDBObject("_id", new ObjectId(id)))).isNotNull();
  // we should not be able to directly delete a child property
  try {
    client.delete(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id + "/foo/bar");
    Fail.fail("We should not be able to directly delete a property on a child object");
  } catch (ResourceNotFoundException e) {
    // expected
  }
  assertThat((DBObject) object).isEqualTo(db.getCollection(methodName).findOne());
}

代码示例来源:origin: liveoak-io/liveoak

@Test
public void deleteChildObject() throws Exception {
  String methodName = "testDeleteChildObject";
  assertThat(db.getCollectionNames().contains(methodName)).isFalse();
  // create the object using the mongo driver directly
  BasicDBObject object = new BasicDBObject();
  object.append("foo", new BasicDBObject("bar", new BasicDBObject("ABC", 123)));
  db.getCollection(methodName).insert(object);
  String id = object.getObjectId("_id").toString();
  assertThat(db.getCollection(methodName).findOne(new BasicDBObject("_id", new ObjectId(id)))).isNotNull();
  // we should not be able to directly delete a child object
  try {
    client.delete(new RequestContext.Builder().returnFields(new DefaultReturnFields("bar(ABC(*))")).build(), "/testApp/" + BASEPATH + "/" + methodName
        + "/" + id + "/foo");
    Fail.fail("We should not be able to directly delete a child object");
  } catch (ResourceNotFoundException e) {
    // expected
  }
  assertThat((DBObject) object).isEqualTo(db.getCollection(methodName).findOne());
}

代码示例来源:origin: liveoak-io/liveoak

@Test
public void directDeleteProperty() throws Exception {
  String methodName = "testDirectDeleteProperty";
  assertThat(db.getCollectionNames().contains(methodName)).isFalse();
  // create the object using the mongo driver directly
  BasicDBObject object = new BasicDBObject();
  object.append("foo", new BasicDBObject("bar", "123"));
  db.getCollection(methodName).insert(object);
  String id = object.getObjectId("_id").toString();
  assertThat(db.getCollection(methodName).findOne(new BasicDBObject("_id", new ObjectId(id)))).isNotNull();
  // we should not be able to directly delete a child property
  try {
    client.delete(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id + "/foo");
    Fail.fail("We should not be able to directly delete a property");
  } catch (ResourceNotFoundException e) {
    // expected
  }
  assertThat((DBObject) object).isEqualTo(db.getCollection(methodName).findOne());
}

代码示例来源:origin: liveoak-io/liveoak

@Test
  public void grandchildDirectUpdate() throws Exception {
    String methodName = "testGrandChildDirectUpdate";
    assertThat(db.getCollection(methodName).getCount()).isEqualTo(0);

    // create the object using the mongo driver directly
    BasicDBObject object = new BasicDBObject();
    object.append("foo", new BasicDBObject("bar", new BasicDBObject("baz", "ABC")));
    db.getCollection(methodName).insert(object);
    assertEquals(1, db.getCollection(methodName).getCount());
    String id = "ObjectId(\"" + object.getObjectId("_id").toString() + "\")";

    // update the resource using the client.update method
    ResourceState resourceState = new DefaultResourceState();
    resourceState.putProperty("baz", "XYZ");

    try {
      client.update(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id + "/foo/bar", resourceState);
      Fail.fail();
    } catch (ResourceNotFoundException e) {
      // expected
    }

    assertThat((DBObject) object).isEqualTo(db.getCollection(methodName).findOne());
  }
}

代码示例来源:origin: liveoak-io/liveoak

@Test
public void getSimple() throws Exception {
  String methodName = "testSimpleGet";
  assertFalse(db.collectionExists(methodName));
  // create the object using the mongo driver directly
  BasicDBObject object = new BasicDBObject();
  object.append("foo", "bar");
  db.getCollection(methodName).insert(object);
  assertEquals(1, db.getCollection(methodName).getCount());
  String id = "ObjectId(\"" + object.getObjectId("_id").toString() + "\")";
  ResourceState result = client.read(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id);
  // verify response
  assertThat(result).isNotNull();
  assertThat(result.getProperty("foo")).isEqualTo("bar");
}

代码示例来源:origin: liveoak-io/liveoak

@Test
public void childDirectUpdate() throws Exception {
  String methodName = "testChildDirectUpdate";
  assertThat(db.getCollection(methodName).getCount()).isEqualTo(0);
  // create the object using the mongo driver directly
  BasicDBObject object = new BasicDBObject();
  object.append("foo", new BasicDBObject("bar", "baz"));
  db.getCollection(methodName).insert(object);
  assertEquals(1, db.getCollection(methodName).getCount());
  String id = "ObjectId(\"" + object.getObjectId("_id").toString() + "\")";
  // update the resource using the client.update method
  ResourceState resourceState = new DefaultResourceState();
  resourceState.putProperty("bar", 123);
  // should not be able to directly update a child object
  try {
    client.update(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id + "/foo", resourceState);
    Fail.fail();
  } catch (CreateNotSupportedException e) {
    // expected
  }
  assertThat((DBObject) object).isEqualTo(db.getCollection(methodName).findOne());
}

代码示例来源:origin: liveoak-io/liveoak

@Test
public void simpleUpdate() throws Exception {
  String methodName = "testSimpleUpdate";
  assertThat(db.getCollection(methodName).getCount()).isEqualTo(0);
  // create the object using the mongo driver directly
  BasicDBObject object = new BasicDBObject();
  object.append("foo", "bar");
  db.getCollection(methodName).insert(object);
  assertEquals(1, db.getCollection(methodName).getCount());
  String id = "ObjectId(\"" + object.getObjectId("_id").toString() + "\")";
  // update the resource using the client.update method
  ResourceState resourceState = new DefaultResourceState();
  resourceState.putProperty("foo", "baz");
  ResourceState result = client.update(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id, resourceState);
  // verify the result
  assertThat(result).isNotNull();
  assertThat(result.id()).isEqualTo(id);
  assertThat(result.getProperty("foo")).isEqualTo("baz");
  // verify db content
  assertThat(db.getCollection(methodName).getCount()).isEqualTo(1);
  DBObject dbObject = db.getCollection(methodName).findOne();
  assertEquals("baz", dbObject.get("foo"));
  assertEquals(new ObjectId(id.substring("ObjectId(\"".length(), id.length() - "\")".length())), dbObject.get("_id"));
}

代码示例来源:origin: liveoak-io/liveoak

@Test
public void simpleDelete() throws Exception {
  String methodName = "testSimpleDelete";
  assertThat(db.getCollectionNames().contains(methodName)).isFalse();
  // create the object using the mongo driver directly
  BasicDBObject object = new BasicDBObject();
  object.append("foo", "bar");
  db.getCollection(methodName).insert(object);
  assertThat(db.getCollection(methodName).getCount()).isEqualTo(1);
  String id = "ObjectId(\"" + object.getObjectId("_id").toString() + "\")";
  // now delete the object
  ResourceState result = client.delete(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id);
  // verify we are getting back the object which was deleted
  assertThat(result).isNotNull();
  assertThat(result.id()).isEqualTo(id);
  assertThat(result.getProperty("foo")).isEqualTo("bar");
  // check that it got deleted in the db
  assertThat(db.getCollection(methodName).getCount()).isEqualTo(0);
}

代码示例来源:origin: liveoak-io/liveoak

@Test
public void getWithEmbedded() throws Exception {
  String methodName = "testGetWithEmbedded";
  assertFalse(db.collectionExists(methodName));
  // create the object using the mongo driver directly
  BasicDBObject object = new BasicDBObject();
  object.append("foo", "bar");
  object.append("child", new BasicDBObject().append("ABC", "XYZ").append("id", "child"));
  db.getCollection(methodName).insert(object);
  assertEquals(1, db.getCollection(methodName).getCount());
  String id = "ObjectId(\"" + object.getObjectId("_id").toString() + "\")";
  ResourceState result = client.read(new RequestContext.Builder().build(), "/testApp/" + BASEPATH + "/" + methodName + "/" + id);
  // verify response
  assertThat(result).isNotNull();
  assertThat(result.getProperty("foo")).isEqualTo("bar");
  ResourceState resultChild = (ResourceState) result.getProperty("child");
  assertThat(resultChild.getProperty("ABC")).isEqualTo("XYZ");
  assertThat(resultChild.getProperty("id")).isEqualTo("child");
  assertThat(resultChild.getProperty("_id")).isNull();
}

代码示例来源:origin: liveoak-io/liveoak

String id = "ObjectId(\"" + object.getObjectId("_id").toString() + "\")";

相关文章