To construct GridFS objects with mongo java driver v3.0 Released - mongodb

Context:
MongoDB 3.0 Released
Java Driver Version 3.0
If I connect to mongodb how it has described into the mongo java driver documentation, how do I obtain a new GridFS object? There are not a constructor for this signature (MongoDatabase db, String bucket).
MongoCredential credential = MongoCredential.createCredential(_userDb, _database, _passDb.toCharArray());
MongoClient mongoClient = new MongoClient(new ServerAddress(_host, _port), Arrays.asList(credential));
MongoDatabase mongoDatabase = mongoClient.getDatabase(_database);
I would like to avoid use deprecated method. It appears that casting is not possible
GridFS gfsPhoto = new GridFS((DB) mongoDatabase, "photos");

Use mongoClient.getDB(_database). GridFS support in the new API didn't make the schedule for the 3.0 schedule but should be in 3.1. For now you're perfectly safe using the old DB API. It's your only choice, really.

Could be good to mention. MongoDB version 3.2 it's changed into GridFSBucket. Took me awhile to find the new solution.
// Create a gridFSBucket using the default bucket name "fs"
GridFSBucket gridFSBucket = GridFSBuckets.create(myDatabase);
// Create a gridFSBucket with a custom bucket name "files"
GridFSBucket gridFSBucket = GridFSBuckets.create(myDatabase, "files");
http://mongodb.github.io/mongo-java-driver/3.2/driver/reference/gridfs/

Related

Is there a way to create a collection with partitionKey/shardKey in Azure Cosmos DB for Mongo API using Java?

I'm trying to create a collection with shard key in Azure Cosmos DB for Mongo API, but this code throws an error as com.microsoft.azure.documentdb.DocumentClientException: Partition key path /partKey is invalid for MongoDB API.
public void createCollections(String collectionNames, String dbName, int throughput, String partitionKey) throws DocumentClientException{
DocumentCollection curCol = new DocumentCollection();
curCol.setId(collectionNames);
ArrayList<String> paths = new ArrayList<String>();
paths.add(partitionKey);
PartitionKeyDefinition partDef = new PartitionKeyDefinition();
partDef.setPaths(paths);
curCol.setPartitionKey(partitionKey);
RequestOptions reqOp = new RequestOptions();
reqOp.setOfferThroughput(throughput);
try (DocumentClient db = this.getDocClient(this.getAccountEndPoint(), this.getAccountKey())) {
db.createCollection(DBLINK + dbName, curCol, reqOp);
}
}
Is there is any way to create one?
You are using the Azure Cosmos DB Java SDK to create a MongoDB resource. That SDK only works with SQL API accounts in Cosmos DB.
To work with MongoDB API resources in Cosmos DB, you need to use native MongoDB drivers. To use these drivers to create and manage MongoDB resources in Cosmos DB you use the MongoDB extension commands for Cosmos DB. These provide the options to specify throughput, shard keys, etc.
Here is a Java example to create a collection for the MongoDB API for Cosmos DB. Apologies for poor Java usage, been a while since I wrote any.
String uri = "<connection string uri>";
MongoClient mongoClient = MongoClients.create(uri)
MongoDatabase database = mongoClient.getDatabase("myDatabase");
Document doc = new Document("customAction", "CreateCollection")
.append("collection", "myCollectionName")
.append("shardKey", "partKey")
.append("offerThroughput", 400);
Document commandResult = database.runCommand(doc);
System.out.println("dbStats: " + commandResult.toJson());

Creating a database and collection programmatically for Azure Cosmos DB via Mongo DB .NET Driver

I am using the .NET MongoDb driver and Azure Cosmos DB Emulator. I am trying to create a database and collection on startup of a dotnet core Web Api project.
I am running the following code within the ConfigureServices function in Startup.cs.
var connectionString = databaseConfig.GetValue<string>("connectionString");
var databaseName = databaseConfig.GetValue<string>("name");
var client = new MongoClient(connectionString);
var db = client.GetDatabase(databaseName);
var collection = db.GetCollection("users");
This is neither creating the database or the collection. I am viewing this in the emulator's data explorer and Robo3T client.
I was under the impression that client.GetDatabase(databaseName) and db.GetCollection<User>("users") will create if the database and collection does not exist respectively.
I can get it to create the database and collection with the following db.CreateCollection("users"). This will require me to check if the collection first exists and isn't the prescribed approach.
Does anyone have any insights into this behaviour?
Thanks.
If you insert a document, the collection gets created, don't ask why!

Mongodb "auth fails" with mongodb php driver and new php library

Using the new mongodb driver: https://github.com/mongodb/mongo-php-driver and the new php library for it: https://github.com/mongodb/mongo-php-library I am getting "auth fails" trying to perform a simple find() query.
In the following code, the connection string follows the pattern mongodb://user:password#mongoinstance:port/database. The connection string works with find() using the old legacy mongo driver, but not the new mongodb driver. The new mongodb is correctly installed in php and displays in phpinfo, the only breaking change we needed to make was to use "new MongoDB\Client" instead of new MongoClient for the legacy mongo driver.
However, when I try to run the following find(), I get auth fails exception in vendor/mongodb/mongodb/src/Operation/Find.php line 179
Using legacy mongo driver there are no problems. Any ideas on the auth fails? What is the mongodb driver failing on exactly? Correct credentials for database and collection are being passed in the mongodb://string. Works in legacy fails with new driver and library.
Environment:
Windows 10
Wamp
PHP 5.5.12
Mongodb driver 1.1.4
Latest version of new php library (Installed with composer: composer require "mongodb/mongodb=^1.0.0")
Mongo instance version 2.4.6
I just had the same error and found out that I had to place the database-name in the connection string.
The documentation here says:
If /database is not specified and the connection string includes credentials, the driver will authenticate to the admin database.
And the user I'm using has no rights to the admin-database, so this is why I received the authentication error.
I advise you to check this too. You can't provide the database-name the same way as with the MongoClient via the connection options.
So here's the solution. After 3 days of banging my head against a wall it turns out the new mongodb driver parses the mongodb uri differently than the legacy mongo driver. My password had a % sign in it. As soon as I changed the % sign to something else everything worked as expected.

How to change bucket name in MongoDB GridFS?

I am using Mongo-Java-Driver 3.0.
How can i change bucket name from fs to new_name in MongoDB GridFS?
Use the bucket argument to the constructor, as documented in the Java GridFS API.
GridFS(db, "new_name")
Is this not working for you? If not, what happens? What's the error?

change my application that works with mongodb on mongolab

I am working on Eclipse with a small scala/scalatra application.. at first I worked with mongodb installed on eclipse (locally) my application is running and everything is good but when I wanted to deploy my application on heroku I found http 503 error.
I think the problem is in my database, is what I need to change my connection with my base mongolab https://mongolab.com or should I add mongolab at heroku.
for connection mongodb I did:
val mongo = MongoConnection()
val coll = mongo("db_test")("tache2")
How can I change my code so I can have it connected to my account mongolab??
You should create your uri like this:
val uri = MongoURI(s"mongodb://${username}:${password}#${host}:${port}/${dbname}")
def db: casbah.MongoDB = MongoConnection(uri)(dbname)
Note, this is using Casbah API.