miss.serena
miss.serena

Reputation: 1180

MongoDB - Difference between getCollection(String name) and getCollectionFromString(String collectionName)?

Can someone please highlight for me the difference between getCollection(String name) and getCollectionFromString(String collectionName) as documented in the MongoDB DB Java api?

public DBCollection getCollectionFromString(String collectionName)
Returns a collection matching a given string.
Parameters: collectionName - the name of the collection
Returns: the collection

and

public DBCollection getCollection(String name)
Gets a collection with a given name.
Parameters: name - the name of the collection to return
Returns: the collection

An illustration with example would be most helpful. (pulled from https://api.mongodb.org/java/3.0/)

Upvotes: 2

Views: 743

Answers (1)

chridam
chridam

Reputation: 103425

Response from Karl Seguin in this forum:

A collection can be identified by a namespace, "db.collection". getCollectionFromString gets the collection for that sort of namespace. getCollection gets it for just the raw collection.

The places where you'd need/use getCollectionFromString have more to do with building some type of mongodb management tool or possibly a CMS which spans multiple databases...that sort of dynamic driven system. getCollection is probably more typical for most apps.

Another explanation:

I think which you use will really depend on what you are expecting your users to enter.

If they don't know anything about all the databases, then using getCollection() makes sense...which I'm thinking would be the case.

If it's a truly dynamic system and the user might say "get me the users from the database app1" then the getCollectionFromStirng might make sense.

However, I'd argue that int the 2nd case, it would be better to simply have two fields: Database and Collection. That way it's more explicit and simpler to deal with. Why should your users know that database and collection are dot separated in MongoDB?

An example to illustrate getCollection(String s):

package com.example.core;

import java.net.UnknownHostException;
import java.util.Set;

import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.Mongo;
import com.mongodb.MongoException;

/**
 * Java : Get collection from MongoDB
 * 
 */
public class GetCollectionApp {
  public static void main(String[] args) {

    try {

        Mongo mongo = new Mongo("localhost", 27017);
        DB db = mongo.getDB("yourdb");

        // get list of collections
        Set<String> collections = db.getCollectionNames();

        for (String collectionName : collections) {
            System.out.println(collectionName);
        }

        // get a single collection
        DBCollection collection = db.getCollection("yourCollection");
        System.out.println(collection.toString());

        System.out.println("Done");

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    }
  }
}

Implementation of getCollectionFromString(String s)

public DBCollection getCollectionFromString(String s){
     DBCollection foo = null;

     while (s.contains(".")){
          int idx = s.indexOf(".");
          String b = s.substring(0, idx);
          s = s.substring(idx + 1);
          foo = getCollection(b);
     }

     if (foo != null)
         return foo;
     return getCollection(s);
 }

Upvotes: 1

Related Questions