简体   繁体   English

使用Java在MongoDB中创建集合

[英]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 我想使用java在mongodb中创建集合。下面是我使用的代码。我可以连接到数据库。但是收集没有发生..请帮助我

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

Indeed you have a compilation error. 确实,你有一个编译错误。

You should use db.getCollection("college") which creates the collection if not exist. 您应该使用db.getCollection("college")来创建集合(如果不存在)。

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. 然后将创建具有单个文档的集合。

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

}

}


}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM