How to create scope and collection programmatically?

Hi Guys,

I am using Java SDK 3.1 with the latest 7.0 Beta server. I cannot figure out how to create scopes and collections programmatically. The code below fails,

JsonObject content = JsonObject.create().put("author", "mike").put("title","My Blog Post 1");
MutationResult result = bucket.scope("s1").collection("c1").upsert("k1", content);
System.out.println(result);

If “s1” and “c1” are not already added through Admin console. This is a show stopper for us. Thanks for your help in advance.

Regards,
Neome

Hi Neome,

You can get a reference to the CollectionManager by calling bucket.collections() . With the collections manager you can create collections and scopes like this:

// create a scope
bucket.collections()
    .createScope("s1");

// create a collection
bucket.collections()
    .createCollection(CollectionSpec.create("c1", "s1"));

Thanks,
David

3 Likes