user3219005
user3219005

Reputation: 83

Create Collection in MongoDB Using Java

i want to create collection in mongodb using java.The below is the code i worked with.I can connect to database.But Collection is not happening..please help me

   import com.mongodb.MongoClient;
   import com.mongodb.DB;
   import com.mongodb.DBCollection;

   public class CreateCollection{

     public static void main( String args[] ){
       try{   

         // To connect to mongodb server
         MongoClient mongoClient = new MongoClient( "localhost" , 27017 );

         // Now connect to your databases
         DB db = mongoClient.getDB( "cms" );
         System.out.println("Connect to database successfully");

         DBCollection school = db.createCollection("college");
         System.out.println("Collection mycol created successfully");

       }catch(Exception e){
         System.err.println( e.getClass().getName() + ": " + e.getMessage() );
       }
    } 
  }

Upvotes: 7

Views: 26803

Answers (4)

Rajan Kumar Yadav
Rajan Kumar Yadav

Reputation: 63

Here I am sharing the working code

import com.mongodb.MongoClient;
import com.mongodb.MongoException;
import com.mongodb.WriteConcern;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.DBCursor;
import com.mongodb.ServerAddress;
import java.util.Arrays;

public class MongoDBCollection
{

    public static void main(String args[])
    {
        try
        {
            // Connect to Database
            MongoClient mongoClient=new MongoClient("localhost", 27017);
            DB db=mongoClient.getDB("analytics");
            System.out.println("Your connection to DB is ready for Use::" + db);
    
            // Create Collection
            DBCollection linked=db.createCollection("LinkedIn", new BasicDBObject()); 
            System.out.println("Collection created successfully");
        }
    
        catch(Exception e)
        {
            System.out.println(e.getClass().getName() + ":" + e.getMessage());
        }
    }
}

Upvotes: 3

stas
stas

Reputation: 11

Here is my way

        MongoCollection collection;
        String collectionName = "somename";
        String jsonObject = "{}";

        if (!mongoTemplate.collectionExists(collectionName)) {
            collection = mongoTemplate.createCollection(collectionName);
            logger.info("Collection %s was successfully created", collectionName);
        } else {
            collection = mongoTemplate.getCollection(collectionName);
        }

        collection.insertOne(Document.parse(jsonObject));

Upvotes: 0

Jared Darling
Jared Darling

Reputation: 108

I just recently needed to do this very thing.

Here is what I used (adapted to your question):

String collectionName = "college");

if(!db.collectionExists(collectionName)
{
  //I can confirm that the collection is created at this point.
  DBCollection school = db.createCollection(collectionName, new BasicDBObject());      
  //I would highly recommend you check the 'school' DBCollection to confirm it was actually created
  System.out.println("Collection %s created successfully", collectionName);
}

Upvotes: 1

Ori Dar
Ori Dar

Reputation: 19020

Indeed you have a compilation error.

You should use db.getCollection("college") which creates the collection if not exist.

Also, the collection is lazily created when you add something to it.

You can add:

school.save(new BasicDBObject("key" , "value"));

The collection with a single document will be created then.

Upvotes: 11

Related Questions