Search in sources :

Example 31 with MongoCollection

use of com.mongodb.client.MongoCollection in project jackrabbit-oak by apache.

the class MongoUtilsTest method createPartialIndex.

@Test
public void createPartialIndex() {
    MongoConnection c = connectionFactory.getConnection();
    c.getDB().dropDatabase();
    MongoStatus status = new MongoStatus(c.getMongoClient(), c.getDBName());
    assumeTrue(status.isVersion(3, 2));
    MongoCollection collection = c.getDatabase().getCollection("test");
    MongoUtils.createPartialIndex(collection, new String[] { "foo", "bar" }, new boolean[] { true, true }, "{foo:true}");
    assertTrue(MongoUtils.hasIndex(collection, "_id"));
    assertTrue(MongoUtils.hasIndex(collection, "foo", "bar"));
    List<Document> indexes = new ArrayList<>();
    collection.listIndexes().into(indexes);
    assertEquals(2, indexes.size());
    for (Document info : indexes) {
        Document key = (Document) info.get("key");
        assertNotNull(key);
        if (key.keySet().contains("foo")) {
            assertEquals(2, key.keySet().size());
            assertEquals(1, key.get("foo"));
            assertEquals(1, key.get("bar"));
            Document filter = (Document) info.get("partialFilterExpression");
            assertNotNull(filter);
            assertEquals(Boolean.TRUE, filter.get("foo"));
        }
    }
    c.getDB().dropDatabase();
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) ArrayList(java.util.ArrayList) Document(org.bson.Document) BsonDocument(org.bson.BsonDocument) MongoConnection(org.apache.jackrabbit.oak.plugins.document.util.MongoConnection) Test(org.junit.Test)

Example 32 with MongoCollection

use of com.mongodb.client.MongoCollection in project jackrabbit-oak by apache.

the class MongoDocumentStore method getStats.

@Nonnull
@Override
public Map<String, String> getStats() {
    ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
    List<MongoCollection<?>> all = ImmutableList.of(nodes, clusterNodes, settings, journal);
    all.forEach(c -> toMapBuilder(builder, db.runCommand(new BasicDBObject("collStats", c.getNamespace().getCollectionName()), BasicDBObject.class), c.getNamespace().getCollectionName()));
    return builder.build();
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MongoCollection(com.mongodb.client.MongoCollection) ImmutableMap(com.google.common.collect.ImmutableMap) Nonnull(javax.annotation.Nonnull)

Example 33 with MongoCollection

use of com.mongodb.client.MongoCollection in project spring-integration by spring-projects.

the class MongoDbOutboundGatewayTests method testWithCollectionCallbackCount.

@Test
@MongoDbAvailable
public void testWithCollectionCallbackCount() throws Exception {
    Message<String> message = MessageBuilder.withPayload("").build();
    MongoDbOutboundGateway gateway = createGateway();
    gateway.setEntityClass(Person.class);
    gateway.setCollectionNameExpression(new LiteralExpression("data"));
    gateway.setCollectionCallback(MongoCollection::count);
    gateway.afterPropertiesSet();
    long result = (long) gateway.handleRequestMessage(message);
    assertEquals(4, result);
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) LiteralExpression(org.springframework.expression.common.LiteralExpression) Test(org.junit.Test) MongoDbAvailable(org.springframework.integration.mongodb.rules.MongoDbAvailable)

Example 34 with MongoCollection

use of com.mongodb.client.MongoCollection in project mongo-java-driver by mongodb.

the class UnifiedCrudHelper method executeBulkWrite.

OperationResult executeBulkWrite(final BsonDocument operation) {
    MongoCollection<BsonDocument> collection = entities.getCollection(operation.getString("object").getValue());
    BsonDocument arguments = operation.getDocument("arguments");
    List<WriteModel<BsonDocument>> requests = arguments.getArray("requests").stream().map(value -> toWriteModel(value.asDocument())).collect(toList());
    BulkWriteOptions options = new BulkWriteOptions();
    for (Map.Entry<String, BsonValue> cur : arguments.entrySet()) {
        switch(cur.getKey()) {
            case "requests":
                break;
            case "ordered":
                options.ordered(cur.getValue().asBoolean().getValue());
                break;
            default:
                throw new UnsupportedOperationException("Unsupported argument: " + cur.getKey());
        }
    }
    return resultOf(() -> toExpected(collection.bulkWrite(requests, options)));
}
Also used : InsertManyResult(com.mongodb.client.result.InsertManyResult) ReplaceOptions(com.mongodb.client.model.ReplaceOptions) DeleteOptions(com.mongodb.client.model.DeleteOptions) BsonDocumentWriter(org.bson.BsonDocumentWriter) MongoDatabase(com.mongodb.client.MongoDatabase) DeleteManyModel(com.mongodb.client.model.DeleteManyModel) WriteModel(com.mongodb.client.model.WriteModel) ChangeStreamDocument(com.mongodb.client.model.changestream.ChangeStreamDocument) BsonValue(org.bson.BsonValue) BsonElement(org.bson.BsonElement) UpdateResult(com.mongodb.client.result.UpdateResult) Arrays.asList(java.util.Arrays.asList) BsonArray(org.bson.BsonArray) Map(java.util.Map) ReadConcern(com.mongodb.ReadConcern) ListCollectionsIterable(com.mongodb.client.ListCollectionsIterable) DeleteOneModel(com.mongodb.client.model.DeleteOneModel) UpdateOptions(com.mongodb.client.model.UpdateOptions) EncoderContext(org.bson.codecs.EncoderContext) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) InsertOneResult(com.mongodb.client.result.InsertOneResult) AggregateIterable(com.mongodb.client.AggregateIterable) ReturnDocument(com.mongodb.client.model.ReturnDocument) CodecRegistries(org.bson.codecs.configuration.CodecRegistries) EstimatedDocumentCountOptions(com.mongodb.client.model.EstimatedDocumentCountOptions) Objects(java.util.Objects) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) Codec(org.bson.codecs.Codec) TimeSeriesOptions(com.mongodb.client.model.TimeSeriesOptions) NotNull(org.jetbrains.annotations.NotNull) ListIndexesIterable(com.mongodb.client.ListIndexesIterable) ReadPreference(com.mongodb.ReadPreference) ValueCodecProvider(org.bson.codecs.ValueCodecProvider) ReplaceOneModel(com.mongodb.client.model.ReplaceOneModel) MongoClient(com.mongodb.client.MongoClient) MongoCollection(com.mongodb.client.MongoCollection) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) BsonString(org.bson.BsonString) Supplier(java.util.function.Supplier) ChangeStreamIterable(com.mongodb.client.ChangeStreamIterable) BsonDocument(org.bson.BsonDocument) ArrayList(java.util.ArrayList) TransactionOptions(com.mongodb.TransactionOptions) FindOneAndDeleteOptions(com.mongodb.client.model.FindOneAndDeleteOptions) MongoCursor(com.mongodb.client.MongoCursor) CreateCollectionOptions(com.mongodb.client.model.CreateCollectionOptions) InsertOneModel(com.mongodb.client.model.InsertOneModel) Objects.requireNonNull(java.util.Objects.requireNonNull) BsonInt64(org.bson.BsonInt64) InsertOneOptions(com.mongodb.client.model.InsertOneOptions) TimeSeriesGranularity(com.mongodb.client.model.TimeSeriesGranularity) BsonCodecProvider(org.bson.codecs.BsonCodecProvider) ListDatabasesIterable(com.mongodb.client.ListDatabasesIterable) UpdateOneModel(com.mongodb.client.model.UpdateOneModel) InsertManyOptions(com.mongodb.client.model.InsertManyOptions) IndexOptions(com.mongodb.client.model.IndexOptions) TimeUnit(java.util.concurrent.TimeUnit) Collectors.toList(java.util.stream.Collectors.toList) ReadConcernLevel(com.mongodb.ReadConcernLevel) DistinctIterable(com.mongodb.client.DistinctIterable) BulkWriteResult(com.mongodb.bulk.BulkWriteResult) DeleteResult(com.mongodb.client.result.DeleteResult) FindOneAndReplaceOptions(com.mongodb.client.model.FindOneAndReplaceOptions) ClientSession(com.mongodb.client.ClientSession) BsonInt32(org.bson.BsonInt32) WriteConcern(com.mongodb.WriteConcern) Collections(java.util.Collections) BsonDocument(org.bson.BsonDocument) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) BsonString(org.bson.BsonString) WriteModel(com.mongodb.client.model.WriteModel) Map(java.util.Map) BsonValue(org.bson.BsonValue)

Example 35 with MongoCollection

use of com.mongodb.client.MongoCollection in project spring-data-mongodb by spring-projects.

the class DbRefMappingMongoConverterUnitTests method convertDocumentWithMapDBRef.

// DATAMONGO-657
@Test
void convertDocumentWithMapDBRef() {
    Document mapValDocument = new Document();
    mapValDocument.put("_id", BigInteger.ONE);
    DBRef dbRef = mock(DBRef.class);
    when(dbRef.getId()).thenReturn(BigInteger.ONE);
    when(dbRef.getCollectionName()).thenReturn("collection-1");
    MongoDatabase dbMock = mock(MongoDatabase.class);
    MongoCollection collectionMock = mock(MongoCollection.class);
    when(dbFactory.getMongoDatabase()).thenReturn(dbMock);
    when(dbMock.getCollection(anyString(), eq(Document.class))).thenReturn(collectionMock);
    FindIterable fi = mock(FindIterable.class);
    when(fi.limit(anyInt())).thenReturn(fi);
    when(fi.sort(any())).thenReturn(fi);
    when(fi.first()).thenReturn(mapValDocument);
    when(collectionMock.find(Mockito.any(Bson.class))).thenReturn(fi);
    MapDBRef mapDBRef = new MapDBRef();
    MapDBRefVal val = new MapDBRefVal();
    val.id = BigInteger.ONE;
    Map<String, MapDBRefVal> mapVal = new HashMap<>();
    mapVal.put("test", val);
    mapDBRef.map = mapVal;
    Document document = new Document();
    converter.write(mapDBRef, document);
    Document map = (Document) document.get("map");
    assertThat(map.get("test")).isInstanceOf(DBRef.class);
    ((Document) document.get("map")).put("test", dbRef);
    MapDBRef read = converter.read(MapDBRef.class, document);
    assertThat(read.map.get("test").id).isEqualTo(BigInteger.ONE);
}
Also used : MongoCollection(com.mongodb.client.MongoCollection) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) DBRef(com.mongodb.DBRef) FindIterable(com.mongodb.client.FindIterable) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase) Bson(org.bson.conversions.Bson) Test(org.junit.jupiter.api.Test)

Aggregations

MongoCollection (com.mongodb.client.MongoCollection)53 Document (org.bson.Document)39 MongoDatabase (com.mongodb.client.MongoDatabase)21 List (java.util.List)16 FindIterable (com.mongodb.client.FindIterable)14 ArrayList (java.util.ArrayList)14 Test (org.junit.Test)11 BasicDBObject (com.mongodb.BasicDBObject)10 Set (java.util.Set)9 ObjectId (org.bson.types.ObjectId)9 MongoCursor (com.mongodb.client.MongoCursor)7 IOException (java.io.IOException)7 HashMap (java.util.HashMap)7 HashSet (java.util.HashSet)7 Bson (org.bson.conversions.Bson)7 Map (java.util.Map)6 Collectors (java.util.stream.Collectors)6 BsonDocument (org.bson.BsonDocument)6 Block (com.mongodb.Block)5 UpdateOptions (com.mongodb.client.model.UpdateOptions)5