0

Problem::
Is there a way to limit a table to one entry only?
I would like to do this from the model so only one entry can be created and modified. Currently I am only allowing the Object.first entry to be modified. I am also open to hear a better practice. Thanks in advance


background::
I am new to Mongo and the only information i found is for creating a new collection.

With Mongo DB you can limit with Capped Collections.
Set limits on mongo db collection

"Mongoid does not provide a mechanism for creating capped collections on the fly - you will need to create these yourself one time up front either with Moped or via the Mongo console."
https://mongoid.github.io/old/en/mongoid/docs/persistence.html

    session.command(create: "name", capped: true, size: 10000000, max: 1000)


Mason SB
  • 445
  • 1
  • 3
  • 13

1 Answers1

1

Depending on what you're trying to achieve, capped collections might not be suited for your use case. In the Capped Collection Documentation, it says:

Capped collections work in a way similar to circular buffers: once a collection fills its allocated space, it makes room for new documents by overwriting the oldest documents in the collection.

If you use a capped collection and then insert a new document, it would just overwrite the existing document, rather than throwing an error. Of course, you could just insert a new document with the updated information instead of overwriting the existing one, but I'm not sure if that's what you intend to do. (If that is helpful, you can create a capped collection through the Mongo Shell when you're setting up your MongoDB instance.)

Overall, it sounds like enforcing this rule in your application logic is the way to go. I would also spend some time thinking about whether you really need this information to be in the database -- would a Ruby singleton class or some environment variables better suit your needs?

egiurleo
  • 106
  • 5