使用 Java 在 MongoDB 中创建集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26214587/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Create Collection in MongoDB Using Java
提问by user3219005
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() );
}
}
}
回答by Ori Dar
Indeed you have a compilation error.
确实,您有编译错误。
You should use db.getCollection("college")
which creates the collection if not exist.
db.getCollection("college")
如果不存在,您应该使用which 创建集合。
Also, the collection is lazily created when you add something to it.
此外,当您向其中添加内容时,该集合是惰性创建的。
You can add:
你可以加:
school.save(new BasicDBObject("key" , "value"));
school.save(new BasicDBObject("key" , "value"));
The collection with a single document will be created then.
然后将创建具有单个文档的集合。
回答by Jared Darling
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);
}
回答by Rajan Kumar Yadav
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());
}
}
}
回答by Stas
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));