Is it possible to create a Collection in MongoDB with no document?
java, mongodb
Solution
You don't really need to create a collection, copy paste from Mongo docs, same principle applies for collections:
To make a connection to a MongoDB, you need to have at the minimum, the name of a database to connect to. The database doesn’t have to exist - if it doesn’t, MongoDB will create it for you.
There are two ways to create a collection. Inserting a document will create the collection if it doesn’t exist or calling the createCollection command.
But if you really want :
db = mongoClient.getDB("mydb");
db.createCollection("testCollection",new BasicDBObject("capped", false));
Documentation link for java driver here: http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/
Problem
i want to create a empty Collection in java. Means a Collection with no document inside. If i create only a Collection the Collection is successfully created, but i can't find the Collection. E.g. with get a List of all my Collections. The empty Collection is not listed. But if i create a empty Collection and when i write a document inside and delete the document the Collection is listed. I think this method is a little bit dirty to create a empty Collection. So have you a better idea? I have writte a programm in MongoDB and i have the functionality to create your Collection independent before you add a document in the Collection.