Reputation: 349
I am using mongo and sails.js. My application allows the user to create a new collection in the mongodb. Since this collection is being created by the user there is no sails model in the models folder therefore I cannot query the new collection (create,update,delete) etc.
Any ideas on how I can accomplish this? I was looking into the Model.native() built in function but there is no model generated when a user creates a new collection so that won't work.
Upvotes: 3
Views: 911
Reputation: 277
That depends in what you want: creating a new model or just a collection.
If you want to generate a new model and access with GET, POST, etc.. like when you create a new API with:
sails generate api foo
you need to run the command and then restart server, because server needs to map models with db collections.
If you want to create a collection consider that you won't be able to access this collection via GET, POST because it's not a model.
MongoDB does not create a database or collection until the first document is inserted. So, if you just want to create a new collection in mongoDB you can run your own script to do it.
This is basically how it works and is by design. When you insert some data the database and collection will be created.
For example you can run a python script with python-shell.
from pymongo import MongoClient
import testdata
from pprint import pprint
client = MongoClient()
db = client.test['collectionDummy']
import datetime
class Temp(testdata.DictFactory):
id = testdata.CountingFactory(10)
number = testdata.CountingFactory(10)
address = testdata.FakeDataFactory('address')
firstName = testdata.FakeDataFactory('firstName')
for document in Temp().generate(2):
result = db.insert_one(document)
Upvotes: 0