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

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());

Related

Azure Function data to Mongo Db

I have created an Azure function which fetches data from a particular API. I want to now insert the data fetch from API in MONGO database but I have no idea how to proceed.
Please help!!
Also let me know if there is any other information needed
First you have to create a mongo db database either in azure or in mongo and get the connection string of the database. In case of azure create a azure cosmos db api for mongo dB resource from the portal and get the connection string.
Theoretically here we create a client object (here we also pass the connection string) which will connect to the mongo db then we will use the client to create and manipulate the database.
Under the quickstart tab of the azure mongo dB you will find language specific ways to create the client and ways to change the database.
Now using the connection string, we can connect to the mongo dB. Since you are using azure function use the application setting to store the connection string and use them as an environment variable. The application setting is under configuration tab.
Refer this article by Robert Walters

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!

connect to documentdb using robomongo

I have a Document DB (using the DocumentDB interface, NOT the MongoDb interface), so the connection string looks like:
AccountEndpoint=https://SomeDatabase.documents.azure.com:443/;AccountKey=xxxxx;
it does NOT look like this:
mongodb://SomeDatabase:xxxxx==#SomeDatabase.documents.azure.com:10255/?ssl=true&replicaSet=globaldb
Question:
How do I connect using RoboMongo or other MongoDb tools/code?
The stuff I looked at said things like take the username (that it shows in the MongoDb version of Cosmos DB (which won't help, as it is a totally different database and the connection string there won't work for apps that need the DocumentDb interface)
Is there a way to do this,or by 'adding support for MongoDB interface to Document DB' like adding the ability to talk to a Ms-SQL Server using MongoDB because you can always download MongoDb an install that on the same machine. (and not be able to get any data passed between them)
When you use Cosmos DB, you must choose, for your deployed database, which API to use with it (DocumentDB, MongoDB, Tables, Gremlin). You cannot use multiple APIs against the same database.
The only way to use MongoDB tools & frameworks is to deploy a Cosmos DB database with the MongoDB API. The MongoDB API is what provides compatibility with MongoDB. Note: The oplog is not provided with the Cosmos MongoDB API, so tools that rely on reading/tailing the oplog will not work.
The DocumentDB API does not surface any of the MongoDB API, so you will not be able to use MongoDB-specific tools when deploying a DocumentDB-specific database.
Note: The MongoDB API of Cosmos does not surface an oplog, so any operations which attempt to query the oplog will not succeed.
Have you seen this how-to by Microsoft for this: Use Robomongo with an Azure Cosmos DB
And one more related: Connecting to Azure Cosmos DB emulator from RoboMongo

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

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/

Mongo admin database credentials don't work in other databases on server

The documentation for Mongo states that when authentication is enabled, and for users added to the admin database, these users should be able to access the other databases in Mongo, with the rights granted at the admin database level.
"The admin database is unique. Users with normal access to the admin database have read and write access to all databases. Users with read only access to the admin database have read only access to all databases." http://docs.mongodb.org/manual/administration/security/
But in testing with the C# library version 1.7.0.4714, this is not the case.
Only accounts created in a specific database have access to that database.
I have tested with credentials on the connection string
and by setting credentials explicitly at the database level in C#
server.GetDatabase(...
new MongoClient(a connectionString ...
Does anyone know if this expected behavior? or can suggest a resolution.
The answer was already posted here on stackoverflow :)
Mongodb C# driver - can't use admin authentication to access other databases
the username used should have (admin) after it to use that account.
This is not a mongodb problem. I am sure that you could authenticate from mongodb shell like this:
use admin
db.auth(user, pass)
This is a mongodb c# driver trick. A long time ago i spent some time to read c# driver code in order to understand this.
So connection string should be like this:
mongodb://admin(admin):1#localhost:27020/myDb
The trick in (admin) in order to tell the driver that you are going to authenticate via admin user.
There is another way to connect with authentication against the admin database.
The downside is that you have to setup the whole connection object instead of packing all info solely on a connection string.
Instead of instantiating the MongoClient with a connection string like
var connectionString = "mongodb://localhost";
var client = new MongoClient(connectionString);
you can create a MongoClientSettings object, set the credentials (along with any other connection settings) and instantiate the client passing that object
string authenticationDB = "admin"
string authenticationUsername = "user"
string authenticationPassword = "pass"
MongoClientSettings settings = new MongoClientSettings();
settings.Credentials = new[] { MongoCredential.CreateMongoCRCredential(authenticationDB, authenticationUsername, authenticationPassword) };
settings.Servers = new[] { new MongoServerAddress("host_1"), new MongoServerAddress("host_2"), new MongoServerAddress("host_3") };
settings.ConnectionMode = ConnectionMode.ReplicaSet;
var client = new MongoClient(settings);
var db = client.GetServer().GetDatabase(database);
http://docs.mongodb.org/ecosystem/tutorial/authenticate-with-csharp-driver/