use of com.mongodb.client.result.UpdateResult in project morphia by mongodb.
the class TestUpdateOperations method testUpdateKeyList.
@Test
public void testUpdateKeyList() {
final ContainsPicKey cpk = new ContainsPicKey();
cpk.name = "cpk one";
Datastore ds = getDs();
ds.save(cpk);
final Pic pic = new Pic();
pic.setName("fist again");
ds.save(pic);
cpk.keys = MorphiaReference.wrap(List.of(pic));
// test with Key<Pic>
Query<ContainsPicKey> query = ds.find(ContainsPicKey.class).filter(eq("name", cpk.name));
final UpdateResult res = query.update(set("keys", cpk.keys)).execute();
assertThat(res.getModifiedCount(), is(1L));
// test reading the object.
final ContainsPicKey cpk2 = ds.find(ContainsPicKey.class).iterator(new FindOptions().limit(1)).next();
assertThat(cpk2, is(notNullValue()));
assertThat(cpk.name, is(cpk2.name));
MatcherAssert.assertThat(cpk2.keys.get(), Matchers.hasItem(pic));
}
use of com.mongodb.client.result.UpdateResult in project morphia by mongodb.
the class TestUpdateOperations method testMinWithDates.
@Test
public void testMinWithDates() {
List<User> entities = List.of(new User("User 1", LocalDate.of(2003, 7, 13)), new User("User 2", LocalDate.of(2009, 12, 1)), new User("User 3", LocalDate.of(2015, 8, 19)));
getDs().save(entities);
UpdateResult updated = getDs().find(User.class).update(min("joined", LocalDate.of(1985, 10, 12))).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 3);
getDs().find(User.class).delete();
getDs().save(entities);
updated = getDs().find(User.class).update(min("joined", Instant.now().minus(5000, DAYS))).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 2);
getDs().find(User.class).delete();
getDs().save(entities);
Calendar instance = Calendar.getInstance();
instance.set(86, Calendar.MAY, 13);
Date date = instance.getTime();
updated = getDs().find(User.class).update(min("joined", date)).execute(new UpdateOptions().multi(true));
assertEquals(updated.getModifiedCount(), 3);
}
use of com.mongodb.client.result.UpdateResult in project morphia by mongodb.
the class TestVersioning method testUpdate.
@Test
public void testUpdate() {
Datastore ds = getDs();
List<VersionedType> initial = List.of(new VersionedType(), new VersionedType());
ds.save(initial);
UpdateResult results = ds.find(VersionedType.class).update(set("text", "some new value")).execute();
assertEquals(results.getModifiedCount(), 1);
List<VersionedType> postUpdate = ds.find(VersionedType.class).filter(eq("text", "some new value")).iterator(new FindOptions().sort(Sort.ascending("_id"))).toList();
for (int i = 0, postUpdateSize = postUpdate.size(); i < postUpdateSize; i++) {
final VersionedType versionedType = postUpdate.get(i);
assertEquals(versionedType.version, initial.get(i).version + 1);
}
}
use of com.mongodb.client.result.UpdateResult in project morphia by mongodb.
the class Employee method main.
public static void main(String[] args) {
final Datastore datastore = Morphia.createDatastore(MongoClients.create(), "morphia_example");
// tell morphia where to find your classes
// can be called multiple times with different packages or classes
datastore.getMapper().mapPackage("dev.morphia.example");
// create the Datastore connecting to the database running on the default port on the local host
datastore.getDatabase().drop();
datastore.ensureIndexes();
final Employee elmer = new Employee("Elmer Fudd", 50000.0);
datastore.save(elmer);
final Employee daffy = new Employee("Daffy Duck", 40000.0);
datastore.save(daffy);
final Employee pepe = new Employee("Pepé Le Pew", 25000.0);
datastore.save(pepe);
elmer.getDirectReports().add(daffy);
elmer.getDirectReports().add(pepe);
datastore.save(elmer);
Query<Employee> query = datastore.find(Employee.class);
final long employees = query.count();
assertEquals(employees, 3);
long underpaid = datastore.find(Employee.class).filter(lte("salary", 30000)).count();
assertEquals(underpaid, 1);
final Query<Employee> underPaidQuery = datastore.find(Employee.class).filter(lte("salary", 30000));
final UpdateResult results = underPaidQuery.update(inc("salary", 10000)).execute();
assertEquals(results.getModifiedCount(), 1);
datastore.find(Employee.class).filter(gt("salary", 100000)).findAndDelete();
}
use of com.mongodb.client.result.UpdateResult in project graylog2-server by Graylog2.
the class V20190805115800_RemoveDashboardStateFromViews method upgrade.
@Override
public void upgrade() {
if (clusterConfigService.get(MigrationCompleted.class) != null) {
LOG.debug("Migration already completed.");
return;
}
final Set<String> legacyViewIds = StreamSupport.stream(viewsCollection.find(exists(FIELD_DASHBOARD_STATE)).spliterator(), false).map(doc -> doc.getObjectId(FIELD_ID)).map(ObjectId::toString).collect(Collectors.toSet());
final UpdateResult updateResult = viewsCollection.updateMany(exists(FIELD_DASHBOARD_STATE), unset(FIELD_DASHBOARD_STATE));
LOG.debug("Migrated " + updateResult.getModifiedCount() + " views.");
clusterConfigService.write(MigrationCompleted.create(updateResult.getModifiedCount(), legacyViewIds));
}
Aggregations