0

I have the next code in lib/ folder

TestCollection = new Mongo.Collection('testCollection');

I expect that this code should create empty collection in MongoDB,but it doesn't.

This collection is created only when I insert some data.

TestCollection = new Mongo.Collection('testCollection');
TestCollection .insert({ foo: 'blah', bar: 'bleh' });

I want to create an empty collection without simultaneously inserting data. Is it possible?

I looked at similar posts,but they insert data immediately after the creation collection. I want first create collection and much later insert data.

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
Lev Kozak
  • 23
  • 1
  • 7
  • 1
    @chridam This is not a duplicate of the other question. The two are totally unrelated, except they are both about Meteor collections. To answer your question, the only way you would be able to satisfy creating an empty collection would be to insert something then immediately remove it. As you already realized, collections are only created when you insert into them. – CodeChimp Jul 22 '16 at 12:37
  • @chridam, the answer may address it, but the other question is totally different. The explanation of a duplicate, based on the above yellow bar, says "This question was marked as an exact duplicate of an existing question." They may be similar, but I'd say these were not exact duplicates of one another as the core questions are completely different. – CodeChimp Jul 22 '16 at 12:48
  • @chridam I guess my point is that the definition of a "duplicate" is that the question is the same. OP would probably never have found this answer because the meta data that would have returned the search results for his question would have most likely buried it since it's not in the title. That's like saying that asking "Is this cup red?" is similar to "How do I determine the color of a cup?". They may both mention the color red, and you could probably answer the first by the second, but they are not the same. I feel the proper process would be to provide a link to the other answer. – CodeChimp Jul 22 '16 at 12:55
  • Possible dupe http://stackoverflow.com/questions/15214667/creating-new-meteor-collections-on-the-fly – chridam Jul 22 '16 at 13:01
  • this is not a duplicate. however, an empty collection is created. why do you think it is not created? – vijayst Jul 22 '16 at 16:21
  • What do you want to archive ? – Antoine Claval Jul 22 '16 at 20:32

2 Answers2

0

From Meteor, no. You can create an empty collection from the mongo shell using db.createCollection

Otherwise just insert a doc and remove it right away as suggested by @CodeChimp

Michel Floyd
  • 18,793
  • 4
  • 24
  • 39
0

same as Michel Floyd say, I have done this in mongodb shell:

> use storybook

> db.main.insert({"fakeKey": "fakeValue"})
WriteResult({ "nInserted" : 1 })

> db.main.find()
{ "_id" : ObjectId("5bd2bddb6ec5fdeabfbd6ecb"), "fakeKey" : "fakeValue" }

> db.main.deleteOne({"_id": ObjectId("5bd2bddb6ec5fdeabfbd6ecb")})
{ "acknowledged" : true, "deletedCount" : 1 }

then via:

db.main.find()

you can find that already added an empty db's collection.

crifan
  • 12,947
  • 1
  • 71
  • 56