Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating capped collection in Meteor #1478

Closed
mattuuh7 opened this issue Oct 9, 2013 · 6 comments
Closed

Creating capped collection in Meteor #1478

mattuuh7 opened this issue Oct 9, 2013 · 6 comments

Comments

@mattuuh7
Copy link

mattuuh7 commented Oct 9, 2013

Is there an existing API for creating capped collection in Meteor? Thanks.

@estark37
Copy link
Contributor

Hi @mattuuh7. There's an underscored (internal, not documented, not guaranteed to stick around in future releases) api for this right now; to create a capped collection on the server, you can do this:

var coll = new Meteor.Collection("myCollection");
coll._createCappedCollection(numBytes);

@mattuuh7
Copy link
Author

Thanks!
It seems the current API only allow createCollection with size parameter, but not max number of documents. Maybe I will create a pull request to have that available as well.

@Slava
Copy link
Contributor

Slava commented Oct 13, 2013

@mattuuh7, Hi there,

consider reading these nuances about live updates to capped collection: https://github.com/meteor/meteor/blob/devel/packages/mongo-livedata/mongo_driver.js#L1161

@fgm
Copy link
Contributor

fgm commented Jun 15, 2016

@yched
Copy link

yched commented Aug 25, 2020

Note - this seems to be broken after Meteor 1.11 and the update to mongodb driver 3.6.0 :

var coll = new Meteor.Collection("myCollection");
coll._createCappedCollection(3000);

This runs fine the very first time, when the collection doesn't actually exist. After that, you'll get an exception:

MongoError: a collection 'meteor.myCollection' already exists
     at Connection.<anonymous> (HOME/.meteor/packages/npm-mongo/.3.8.0.1u7psm6.649z++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb/lib/core/connection/pool.js:451:61)
     at Connection.emit (events.js:315:20)
     at Connection.EventEmitter.emit (domain.js:483:12)
     at processMessage (HOME/.meteor/packages/npm-mongo/.3.8.0.1u7psm6.649z++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb/lib/core/connection/connection.js:452:10)
     at Socket.<anonymous> (HOME/.meteor/packages/npm-mongo/.3.8.0.1u7psm6.649z++os+web.browser+web.browser.legacy+web.cordova/npm/node_modules/mongodb/lib/core/connection/connection.js:621:15)
     at Socket.emit (events.js:315:20)
     at Socket.EventEmitter.emit (domain.js:483:12)
     at addChunk (_stream_readable.js:295:12)
     at readableAddChunk (_stream_readable.js:271:9)
     at Socket.Readable.push (_stream_readable.js:212:10)
     at TCP.onStreamRead (internal/stream_base_commons.js:186:23) 

I suspect it's because of the "Changes in behavior of Db.prototype.createCollection" section in
https://github.com/mongodb/node-mongodb-native/releases/tag/v3.6.0

@yched
Copy link

yched commented Aug 26, 2020

As suggested in the Meteor forums, the following workaround works:

const db = Meteor.users.rawDatabase();
const collectionExists = db.listCollections({ name: 'myCollection' }).hasNext().await();
if (!collectionExists) {
  db.createCollection('myCollection', { capped: true, size: 3000 }).await();
}
const coll = new Mongo.Collection('myCollection');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants