0

I am new to meteor. Can anyone please tell me how to make a database connection and how to access data from the database?
I have a clone of the GitHub application working fine for me but not understand where in the database connection file and where database tables exist and files are linked to access the data.

Thanks in advance.

3 Answers 3

2

Database connection and location

Meteor projects use MongoDB by default out of the box using the mongo core package.

The connection mode and target are determined by environment variables.

  • MONGO_URL
    (development, production)

    MongoDB server URL. Give a fully qualified URL (or comma-separated list of URLs) like MONGO_URL="mongodb://user:[email protected]:10139". For more information see the MongoDB docs.

  • MONGO_OPLOG_URL
    (development, production)

    MongoDB server oplog URL. If you’re using a replica set (which you should), construct this url like so: MONGO_OPLOG_URL="mongodb://user:[email protected]:10139/local?replicaSet=(your replica set)&authSource=(your auth source)"

The MONGO_URL is required in a built application (normally running in production).

In development mode (i.e, run by the meteor run command, which is likely the case in your scenario), if the above environment variables are not set, the Meteor utility creates a default database (stored inside the .meteor directory) and runs it at the next available port following your app's port (e.t, on the default port 3000, the DB will be available at port 3001).

The meteor mongo -U command prints out the mongo connection string. It is typically something like mongodb://127.0.0.1:3001/meteor for the default settings.

This means you can access it from your console (BTW, the meteor mongo command gives you a DB prompt) or a GUI database inspector.

The MONGO_OPLOG_URL is used for enhancing performance.

Collection creation

The mongo package does not create collections if there is no need to, so simply declaring a Mongo.Collection in your code will not immediately create it. Some operations trigger the creation of a non-existing collections. For example, inserting a document or creating an index.

Sign up to request clarification or add additional context in comments.

Comments

1

Anytime you create an instance of Mongo.Collection it connects to the MongoDB collection/table of the same name. If that table doesn't exist, it will be created for you when you insert your first document/row.

I strongly recommend the official meteor tutorial if you are new to meteor

Comments

0

it's so easy to handle collections with mongoDB in meteor I recommend you to read the meteor documentation https://docs.meteor.com/api/collections.html

or you can see the meteor tutorials https://www.meteor.com/tutorials/blaze/collections

or you can see this video https://www.youtube.com/watch?v=AdbJPTsE2gQ&t=84s

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.