- Meteor - Best Practices
- Meteor - ToDo App
- Meteor - Running on mobile
- Meteor - Deployment
- Meteor - Structure
- Meteor - Publish & Subscribe
- Meteor - Package.js
- Meteor - Methods
- Meteor - Accounts
- Meteor - Sorting
- Meteor - Security
- Meteor - Assets
- Meteor - Email
- Meteor - HTTP
- Meteor - EJSON
- Meteor - Timers
- Meteor - Blaze
- Meteor - Check
- Meteor - Core API
- Meteor - Packages
- Meteor - Tracker
- Meteor - Session
- Meteor - Events
- Meteor - Forms
- Meteor - Collections
- Meteor - Templates
- Meteor - First Application
- Meteor - Environment Setup
- Meteor - Overview
- Meteor - Home
Meteor Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Meteor - Collections
In this chapter, we will learn how to use MongoDB collections.
Create a Collection
We can create a new collection with the following code −
meteorApp.js
MyCollection = new Mongo.Collection( myCollection );
Add Data
Once the collection is created, we can add data by using the insert method.
meteorApp.js
MyCollection = new Mongo.Collection( myCollection ); var myData = { key1: "value 1...", key2: "value 2...", key3: "value 3...", key4: "value 4...", key5: "value 5..." } MyCollection.insert(myData);
Find Data
We can use the find method to search for data in the collection.
meteorApp.js
MyCollection = new Mongo.Collection( myCollection ); var myData = { key1: "value 1...", key2: "value 2...", key3: "value 3...", key4: "value 4...", key5: "value 5..." } MyCollection.insert(myData); var findCollection = MyCollection.find().fetch(); console.log(findCollection);
The console will show the data we inserted previously.
![Meteor Collection Find](/meteor/images/meteor-collections-find.jpg)
We can get the same result by adding the search parameters.
meteorApp.js
MyCollection = new Mongo.Collection( myCollection ); var myData = { key1: "value 1...", key2: "value 2...", key3: "value 3...", key4: "value 4...", key5: "value 5..." } MyCollection.insert(myData); var findCollection = MyCollection.find({key1: "value 1..."}).fetch(); console.log(findCollection);
Update Data
The next step is to update our data. After we have created a collection and inserted new data, we can use the update method.
meteorApp.js
MyCollection = new Mongo.Collection( myCollection ); var myData = { key1: "value 1...", key2: "value 2...", key3: "value 3...", key4: "value 4...", key5: "value 5..." } MyCollection.insert(myData); var findCollection = MyCollection.find().fetch(); var myId = findCollection[0]._id; var updatedData = { key1: "updated value 1...", key2: "updated value 2...", key3: "updated value 3...", key4: "updated value 4...", key5: "updated value 5..." } MyCollection.update(myId, updatedData); var findUpdatedCollection = MyCollection.find().fetch(); console.log(findUpdatedCollection);
The console will show that our collection is updated.
![Meteor Collections Update](/meteor/images/meteor-collections-update.jpg)
Delete Data
Data can be deleted from the collection using the remove method. We are setting id in this example as a parameter to delete specific data.
meteorApp.js
MyCollection = new Mongo.Collection( myCollection ); var myData = { key1: "value 1...", key2: "value 2...", key3: "value 3...", key4: "value 4...", key5: "value 5..." } MyCollection.insert(myData); var findCollection = MyCollection.find().fetch(); var myId = findCollection[0]._id; MyCollection.remove(myId); var findDeletedCollection = MyCollection.find().fetch(); console.log(findDeletedCollection);
The console will show an empty array.
![Meteor Collections Remove](/meteor/images/meteor-collections-remove.jpg)
If we want to delete everything from the collection, we can use the same method, however, instead of id we will use an empty object {}. We need to do this on the server for security reasons.
meteorApp.js
if (Meteor.isServer) { MyCollection = new Mongo.Collection( myCollection ); var myData = { key1: "value 1...", key2: "value 2...", key3: "value 3...", key4: "value 4...", key5: "value 5..." } MyCollection.insert(myData); MyCollection.remove({}); var findDeletedCollection = MyCollection.find().fetch(); console.log(findDeletedCollection); }
We can also delete data using other parameters. As in the previous example, Meteor will force us to do this from the server.
meteorApp.js
if (Meteor.isServer) { MyCollection = new Mongo.Collection( myCollection ); var myData = { key1: "value 1...", key2: "value 2...", key3: "value 3...", key4: "value 4...", key5: "value 5..." } MyCollection.insert(myData); MyCollection.remove({key1: "value 1..."}); var findDeletedCollection = MyCollection.find().fetch(); console.log(findDeletedCollection); }
It can be seen that the data is deleted from the command window.
![Meteor Collections Remove Server](/meteor/images/meteor-collections-remove-server.jpg)