Best way to keep MongoDB collection schema in external file - mongodb

What is the best way to keep MongoDB collection schema (using mongoose) in external file and access them from main app.js considering n number of schema?

You can have a separate directory models. And have your db models there. If you want you can follow MVC. Here is what your directory structure can be like.
./project_dir
app.js
models
views
routes
package.json
In models you have files where you have your db models (files where you have your schema).
You can also have a look at this sample app at github

Related

mongodb - strategy from having relational DB CSV dump imported to highly denormalised mongodb documents

We want to migrate data to mongodb using a CSV files dump created out of a teradata. Data needs to be refreshed in mongodb every night from a fresh teradata csv dump
Approach we are thinking of is:
Get the CSV files exported out of relational db. They are going to be very similar to table structure in relational db
Import the CSV files into mongodb staging collections subsequently, which will be mirroring relational db structure in terms of being normalised. This may be done using say mongoimport in overnight batches. This is going to result in many collections as we are thinking to import each 'type' of CSV into its own collection e.g. Customers.csv and Accounts.csv will result in two respective collections of same name.
Create de-normalised collections out of staging collections, ready to be exposed to UI. Run some schema migration script, which queries the staging collections and creates more denormalised and fewer collections ready for use in the application UI.Eg, Customers and Accounts collections, after running migration script should result in a third collection say AccountCustomers collection where Each account doc has embedded customers array (denormalised and no need of joins when UI needs data)
Question: Is there a better strategy , as all above steps are expected to complete nightly, every night?
Question: Is mongoimport OK to use for importing the CSV files in nightly batches.
Question: What is the best way to migrate (denormalise) collections within the same mongo instance?Eg we have stagingdb having collections customers and accounts and we want to reach a state where we have proddb having collection accountcustomers

Export MongoDB database/collection structure as JavaScript

I have an existing database with collection, indexes and data. I now want to deploy database to another location without any data (just collection and indexes). I fail to find a simple way to recreate basic database structure w/o data.
Is there a command/tool that exports an existing database/collection as a series of JavaScript commands such as below? I can't seem to find anything related to this. I am currently using Robomongo as a management/test/query tool.
db.createCollection('test');
db.test.createIndex('index', ...);
// etc...

Do you use nested document or multiple documents connected by IDs in MongoDB?

I have a "project" entity and each project can have many "models" (files and some metadata) and each model is associated with a "user".
So far I have considered each of the entities as a document in MongoDB, However, I find myself going back to SQl style when trying to create the relationships between them (e.g. adding the project ID in each model's metadata). Is this okay in the NoSQL world? Should I have a nested document for project which contains a list of all linked models? If yes, how can I do that considering that I use GridFs for storing the model files, while the project is just a normal document?
In MongoDB your data model should be such that the most frequently accessed queries should be blazing fast. In this scenario , since you are using GridFS for storing model files, you can store project,user etc as metadata for a GridFS model entry. You can then query 'metadata' and GridFS together. You can refer to this for specifics : GRIDFS Metadata

Unknown Database Content

I was given the data files to MongoDB without being told much of what was in the content. Is there a way to probe the contents of the database to identify commonalities within the database.
There are a few different mongo shell helpers that will give you an idea of the general structure of collections (eg. field/data types and their frequency of usage in a collection):
schema.js
variety
To get a better idea of the actual content, you could try one of the Admin UIs.

Creting collection only on MongoDB

I want to create only the collections structure.
i.e.
Say Products collection contains a list of Categories.
I want to specify this container structure by creating this dependencies, but I do not want to create any collection entry (say there is a loader program somewhere that bulk uploads the data).
The closet analogy in RDBMS is; DBA creates the schema design with constraints and dependencies; application or ETL tool loads the actual data.
Most of the examples that I see simply create a sample collection and then invoke the
db.insert(document)
OR
db.save(document)
Is it even possible in MongoDB?
if the question is not clear, please let me know.
Thanks
The short answer is NO.
You cannot create a schema in MongoDB. A collection is just a set of documents. Furthermore, dependencies are likely to be represented with embedded documents (as opposed to referenced documents).
We can be more specific if you post the data you want to represent.