Search in sources :

Example 46 with MongoCollection

use of com.mongodb.client.MongoCollection in project wildfly-swarm by wildfly-swarm.

the class StatefulTestBean method addUserComment.

public String addUserComment() {
    MongoCollection collection = null;
    Document query = null;
    try {
        // add a comment from user Melanie
        String who = "Melanie";
        Document comment = new Document("_id", who).append("name", who).append("address", new BasicDBObject("street", "123 Main Street").append("city", "Fastville").append("state", "MA").append("zip", 18180)).append("comment", "I really love your new website but I have a lot of questions about using NoSQL versus a traditional RDBMS.  " + "I would like to sign up for your 'MongoDB Is Web Scale' training session.");
        // save the comment
        collection = database.getCollection("comments");
        collection.insertOne(comment);
        // look up the comment from Melanie
        query = new Document("_id", who);
        FindIterable cursor = collection.find(query);
        Object userComment = cursor.first();
        return userComment.toString();
    } finally {
        collection.drop();
    }
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) MongoCollection(com.mongodb.client.MongoCollection) JsonObject(javax.json.JsonObject) BasicDBObject(com.mongodb.BasicDBObject) FindIterable(com.mongodb.client.FindIterable) Document(org.bson.Document)

Example 47 with MongoCollection

use of com.mongodb.client.MongoCollection in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManager method select.

@Override
public List<DocumentEntity> select(DocumentQuery query) {
    String collectionName = query.getDocumentCollection();
    MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
    Bson mongoDBQuery = query.getCondition().map(DocumentQueryConversor::convert).orElse(EMPTY);
    return stream(collection.find(mongoDBQuery).spliterator(), false).map(MongoDBUtils::of).map(ds -> DocumentEntity.of(collectionName, ds)).collect(toList());
}
Also used : Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) MongoCollection(com.mongodb.client.MongoCollection) MongoDatabase(com.mongodb.client.MongoDatabase) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) BsonDocument(org.bson.BsonDocument) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Bson(org.bson.conversions.Bson) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) DocumentCollectionManager(org.jnosql.diana.api.document.DocumentCollectionManager) Documents(org.jnosql.diana.api.document.Documents) StreamSupport.stream(java.util.stream.StreamSupport.stream) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) DeleteResult(com.mongodb.client.result.DeleteResult) ID_FIELD(org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) BsonDocument(org.bson.BsonDocument) Bson(org.bson.conversions.Bson)

Example 48 with MongoCollection

use of com.mongodb.client.MongoCollection in project jnosql-diana-driver by eclipse.

the class MongoDBDocumentCollectionManager method update.

@Override
public DocumentEntity update(DocumentEntity entity) {
    DocumentEntity copy = entity.copy();
    String collectionName = entity.getName();
    MongoCollection<Document> collection = mongoDatabase.getCollection(collectionName);
    Document id = copy.find(ID_FIELD).map(d -> new Document(d.getName(), d.getValue().get())).orElseThrow(() -> new UnsupportedOperationException("To update this DocumentEntity " + "the field `id` is required"));
    copy.remove(ID_FIELD);
    collection.findOneAndReplace(id, getDocument(entity));
    return entity;
}
Also used : Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) MongoCollection(com.mongodb.client.MongoCollection) MongoDatabase(com.mongodb.client.MongoDatabase) DocumentDeleteQuery(org.jnosql.diana.api.document.DocumentDeleteQuery) BsonDocument(org.bson.BsonDocument) DocumentQuery(org.jnosql.diana.api.document.DocumentQuery) Bson(org.bson.conversions.Bson) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) DocumentCollectionManager(org.jnosql.diana.api.document.DocumentCollectionManager) Documents(org.jnosql.diana.api.document.Documents) StreamSupport.stream(java.util.stream.StreamSupport.stream) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Duration(java.time.Duration) DeleteResult(com.mongodb.client.result.DeleteResult) ID_FIELD(org.jnosql.diana.mongodb.document.MongoDBUtils.ID_FIELD) DocumentEntity(org.jnosql.diana.api.document.DocumentEntity) Document(org.bson.Document) MongoDBUtils.getDocument(org.jnosql.diana.mongodb.document.MongoDBUtils.getDocument) BsonDocument(org.bson.BsonDocument)

Example 49 with MongoCollection

use of com.mongodb.client.MongoCollection in project presto by prestodb.

the class MongoSession method createTableMetadata.

private void createTableMetadata(SchemaTableName schemaTableName, List<MongoColumnHandle> columns) throws TableNotFoundException {
    String schemaName = schemaTableName.getSchemaName();
    String tableName = schemaTableName.getTableName();
    MongoDatabase db = client.getDatabase(schemaName);
    Document metadata = new Document(TABLE_NAME_KEY, tableName);
    ArrayList<Document> fields = new ArrayList<>();
    if (!columns.stream().anyMatch(c -> c.getName().equals("_id"))) {
        fields.add(new MongoColumnHandle("_id", OBJECT_ID, true).getDocument());
    }
    fields.addAll(columns.stream().map(MongoColumnHandle::getDocument).collect(toList()));
    metadata.append(FIELDS_KEY, fields);
    MongoCollection<Document> schema = db.getCollection(schemaCollection);
    schema.createIndex(new Document(TABLE_NAME_KEY, 1), new IndexOptions().unique(true));
    schema.insertOne(metadata);
}
Also used : Document(org.bson.Document) Arrays(java.util.Arrays) LoadingCache(com.google.common.cache.LoadingCache) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) Date(java.util.Date) MongoDatabase(com.mongodb.client.MongoDatabase) TypeSignature(com.facebook.presto.common.type.TypeSignature) SchemaTableName(com.facebook.presto.spi.SchemaTableName) SchemaNotFoundException(com.facebook.presto.spi.SchemaNotFoundException) Map(java.util.Map) Collectors.toSet(java.util.stream.Collectors.toSet) ImmutableSet(com.google.common.collect.ImmutableSet) DOUBLE(com.facebook.presto.common.type.DoubleType.DOUBLE) Set(java.util.Set) Collectors(java.util.stream.Collectors) Range(com.facebook.presto.common.predicate.Range) Preconditions.checkState(com.google.common.base.Preconditions.checkState) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) FindIterable(com.mongodb.client.FindIterable) Optional(java.util.Optional) CacheBuilder(com.google.common.cache.CacheBuilder) IntStream(java.util.stream.IntStream) Logger(com.facebook.airlift.log.Logger) StandardTypes(com.facebook.presto.common.type.StandardTypes) MongoCollection(com.mongodb.client.MongoCollection) Slice(io.airlift.slice.Slice) TypeSignatureParameter(com.facebook.presto.common.type.TypeSignatureParameter) OBJECT_ID(com.facebook.presto.mongodb.ObjectIdType.OBJECT_ID) TIMESTAMP(com.facebook.presto.common.type.TimestampType.TIMESTAMP) MINUTES(java.util.concurrent.TimeUnit.MINUTES) PrestoException(com.facebook.presto.spi.PrestoException) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ImmutableList(com.google.common.collect.ImmutableList) MongoCursor(com.mongodb.client.MongoCursor) Verify.verify(com.google.common.base.Verify.verify) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) TypeManager(com.facebook.presto.common.type.TypeManager) Objects.requireNonNull(java.util.Objects.requireNonNull) BOOLEAN(com.facebook.presto.common.type.BooleanType.BOOLEAN) Type(com.facebook.presto.common.type.Type) NamedTypeSignature(com.facebook.presto.common.type.NamedTypeSignature) BIGINT(com.facebook.presto.common.type.BigintType.BIGINT) IndexOptions(com.mongodb.client.model.IndexOptions) Throwables.throwIfInstanceOf(com.google.common.base.Throwables.throwIfInstanceOf) Domain(com.facebook.presto.common.predicate.Domain) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) Collectors.toList(java.util.stream.Collectors.toList) TableNotFoundException(com.facebook.presto.spi.TableNotFoundException) ColumnHandle(com.facebook.presto.spi.ColumnHandle) MongoClient(com.mongodb.MongoClient) DeleteResult(com.mongodb.client.result.DeleteResult) ObjectId(org.bson.types.ObjectId) HOURS(java.util.concurrent.TimeUnit.HOURS) RowFieldName(com.facebook.presto.common.type.RowFieldName) VisibleForTesting(com.google.common.annotations.VisibleForTesting) IndexOptions(com.mongodb.client.model.IndexOptions) ArrayList(java.util.ArrayList) Document(org.bson.Document) MongoDatabase(com.mongodb.client.MongoDatabase)

Example 50 with MongoCollection

use of com.mongodb.client.MongoCollection in project legendarybot by greatman.

the class CustomCommandsPlugin method start.

@Override
public void start() {
    log.info("Loading custom commands");
    getBot().getJDA().forEach(jda -> jda.getGuilds().forEach(g -> {
        MongoCollection<Document> collection = getBot().getMongoDatabase().getCollection(MONGO_COLLECTION_NAME);
        Map<String, String> result = new HashMap<>();
        collection.find(eq("guild_id", g.getId())).forEach((Block<Document>) document -> {
            if (document.containsKey(MONGO_DOCUMENT_NAME)) {
                ((Document) document.get(MONGO_DOCUMENT_NAME)).forEach((k, v) -> {
                    System.out.println("GUILD:" + g.getId() + " key: " + k + " value: " + v);
                    result.put(k, (String) v);
                });
            }
        });
        guildCustomCommands.put(g.getId(), result);
    }));
    getBot().getJDA().forEach(jda -> jda.addEventListener(listener));
    log.info("Custom commands loaded");
    getBot().getCommandHandler().setUnknownCommandHandler(new IUnknownCommandHandler(this));
    getBot().getCommandHandler().addCommand("createcmd", new CreateCommand(this), "Custom Commands Admin Commands");
    getBot().getCommandHandler().addCommand("removecmd", new RemoveCommand(this), "Custom Commands Admin Commands");
    getBot().getCommandHandler().addCommand("listcommands", new ListCommand(this), "General Commands");
    log.info("Plugin Custom Commands loaded!");
    log.info("Command !createcmd loaded!");
}
Also used : Document(org.bson.Document) CreateCommand(com.greatmancode.legendarybot.plugin.customcommands.commands.CreateCommand) MongoCollection(com.mongodb.client.MongoCollection) LegendaryBotPlugin(com.greatmancode.legendarybot.api.plugin.LegendaryBotPlugin) HashMap(java.util.HashMap) Filters.exists(com.mongodb.client.model.Filters.exists) Updates.set(com.mongodb.client.model.Updates.set) ListCommand(com.greatmancode.legendarybot.plugin.customcommands.commands.ListCommand) PluginWrapper(org.pf4j.PluginWrapper) RemoveCommand(com.greatmancode.legendarybot.plugin.customcommands.commands.RemoveCommand) Guild(net.dv8tion.jda.core.entities.Guild) Filters.and(com.mongodb.client.model.Filters.and) Block(com.mongodb.Block) Map(java.util.Map) Filters.eq(com.mongodb.client.model.Filters.eq) Collections(java.util.Collections) UpdateOptions(com.mongodb.client.model.UpdateOptions) Updates.unset(com.mongodb.client.model.Updates.unset) RemoveCommand(com.greatmancode.legendarybot.plugin.customcommands.commands.RemoveCommand) MongoCollection(com.mongodb.client.MongoCollection) CreateCommand(com.greatmancode.legendarybot.plugin.customcommands.commands.CreateCommand) ListCommand(com.greatmancode.legendarybot.plugin.customcommands.commands.ListCommand) Block(com.mongodb.Block) Document(org.bson.Document) HashMap(java.util.HashMap) Map(java.util.Map)

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