Inserting Records into Collections
Explore how to insert records into MongoDB collections within Meteor.js applications. Learn to structure documents, use the insert method with callbacks, and manage data consistency in client-only collections. By the end, you will handle inserting data efficiently and display updates without page reloads.
We'll cover the following...
Inserting data into a collection involves putting documents into the MongoDB database. MongoDB doesn’t enforce a schema (a document blueprint for the collection), but it’s important that we structure our documents in ways that reduce the amount of data retrieval code. Later, we’ll write to retrieve data.
Syntax for inserting data
We can insert the data in Meteor.js by writing Mongo.Collection.insert(dataToInsert, callback).
Note: To insert a document, a collection must exist that it can be inserted into.
A callback function has two parameters, which are the error object and the data (return value). If there’s no error, the error object is null. This follows a convention in Node.js that ...