简体   繁体   中英

Can not create empty collection in MongoDB using Meteor

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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