Book Image

Building Single-page Web Apps with Meteor

By : Fabian Vogelsteller
Book Image

Building Single-page Web Apps with Meteor

By: Fabian Vogelsteller

Overview of this book

Table of Contents (21 chapters)
Building Single-page Web Apps with Meteor
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Updating a collection


Now that we know how to insert and fetch data, let's take a look at how to update data in our database.

As we've already seen before, we can use the console of our browser to play with the database. For our next examples, we will use only the console to see how Meteor reactively changes the templates when we change data.

To be able to edit a post in our database, we first need to know the _id field of its entry. To find this out, we need to type the following command:

Posts.find().fetch();

This will return us all the documents in the Posts collection, as we are not passing any specific query object.

In the returned array, we need to take a look at the last item, with the My Fifth entry title, and copy the _id field to the clipboard using Cmd + C (or Ctrl + C if we're on Windows or Linux).

Note

We can also simply use Posts.findOne(), which will give us the first document it finds.

Now that we have _id, we can simply update the title of our fifth post by typing the following...