Search in sources :

Example 31 with UpdateResult

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));
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) ContainsPic(dev.morphia.test.query.TestQuery.ContainsPic) Pic(dev.morphia.test.query.TestQuery.Pic) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.testng.annotations.Test)

Example 32 with UpdateResult

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);
}
Also used : User(dev.morphia.test.models.User) Calendar(java.util.Calendar) UpdateResult(com.mongodb.client.result.UpdateResult) UpdateOptions(dev.morphia.UpdateOptions) Date(java.util.Date) LocalDate(java.time.LocalDate) UpdateOperators.currentDate(dev.morphia.query.experimental.updates.UpdateOperators.currentDate) Test(org.testng.annotations.Test)

Example 33 with UpdateResult

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);
    }
}
Also used : FindOptions(dev.morphia.query.FindOptions) Datastore(dev.morphia.Datastore) Morphia.createDatastore(dev.morphia.Morphia.createDatastore) UpdateResult(com.mongodb.client.result.UpdateResult) Test(org.testng.annotations.Test)

Example 34 with UpdateResult

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();
}
Also used : Datastore(dev.morphia.Datastore) UpdateResult(com.mongodb.client.result.UpdateResult)

Example 35 with UpdateResult

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));
}
Also used : UpdateResult(com.mongodb.client.result.UpdateResult)

Aggregations

UpdateResult (com.mongodb.client.result.UpdateResult)75 Document (org.bson.Document)32 Bson (org.bson.conversions.Bson)17 Test (org.junit.Test)12 Update (org.springframework.data.mongodb.core.query.Update)12 Test (org.testng.annotations.Test)11 UpdateOptions (com.mongodb.client.model.UpdateOptions)10 Test (org.junit.jupiter.api.Test)10 BasicDBObject (com.mongodb.BasicDBObject)9 DeleteResult (com.mongodb.client.result.DeleteResult)8 FindOptions (dev.morphia.query.FindOptions)8 BsonDocument (org.bson.BsonDocument)8 Query (org.springframework.data.mongodb.core.query.Query)8 FindOneAndUpdateOptions (com.mongodb.client.model.FindOneAndUpdateOptions)7 ArrayList (java.util.ArrayList)6 ObjectId (org.bson.types.ObjectId)6 UpdateOptions (dev.morphia.UpdateOptions)5 Map (java.util.Map)5 Exchange (org.apache.camel.Exchange)5 Processor (org.apache.camel.Processor)5