【问题标题】:Create Collection in MongoDB Using Java使用 Java 在 MongoDB 中创建集合
【发布时间】:2014-10-06 10:45:52
【问题描述】:

我想使用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() );
       }
    } 
  }

【问题讨论】:

  • 你有什么错误吗?
  • 你在 DBCollection school = db.createCollection("college");根据需要:String,DBObject 但找到:String
  • 你可以试试 DBCollection school = db.createCollection("college",null);
  • @KumarKailash:我试过了,但它显示了这样的错误 必需:String,DBObject .. but found:String , null

标签: java mongo-collection


【解决方案1】:

确实有编译错误。

如果不存在,您应该使用 db.getCollection("college") 创建集合。

此外,当您向其中添加内容时,该集合是延迟创建的。

您可以添加:

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

然后将创建具有单个文档的集合。

【讨论】:

  • 我不想插入文档只是想创建集合
  • 好吧,如前所述,如果您不添加任何内容,它就不是真正创建的。如果您不放任何东西,那么创建集合就没有意义。如果您坚持可以创建文档,然后执行remove 操作。然后你会看到空集合
  • 实际上我的目标是创建一个空集合,我将从 UI 的外部用户条目插入文档
  • 因此,对于每个插入,您都将调用db.getCollection("college"),如果不存在,它将创建集合。为此,您不需要上面的代码。
  • :那我可以帮我做一个上面的示例
【解决方案2】:

我在这里分享工作代码

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

}

}


}

【讨论】:

    【解决方案3】:

    我只是最近需要做这件事。

    这是我使用的(适应您的问题):

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

    【讨论】:

      【解决方案4】:

      这是我的方式

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 2012-09-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多