Build Applications with Meteor by Dobrin Ganev

Build Applications with Meteor by Dobrin Ganev

Author:Dobrin Ganev
Language: eng
Format: epub, pdf
Publisher: Packt Publishing
Published: 2017-05-29T09:14:04+00:00


Importing the data

They are many ways of importing existing data into MongoDB. It depends on many factors, but the two most important ones are the source and data model. Our data sample is in JSON format, which is the format of the documents stored in MongoDB collections. The way MongoDB works is that behind the scenes, it encodes the JSON documents in a binary format—BSON (Binary JSON)—with some additional metadata, and when we query the collections, we get plain JSON objects. JSON is what we, JavaScript developers, naturally think of data that makes MongoDB extremely easy to work with; this is also the main reason why it is one of the most popular NoSQL databases.

How we model our collections is completely different from how we'd model our tables in a relational database. Data modeling in MongoDB is a big topic and it's out of the scope of this book; however, we actually did a bit of data modeling in our Redux stores in the previous chapters. We started modeling our stores with the question, "what state our application needs to persist?" and the way we think in MongoDB is, "what data does our application UI need?" In both cases, we start data modeling from the UI, which is the opposite of how we would usually do data modeling in relational databases.

For this app, we can do a direct import of the sample JSON dataset into one collection and build our frontend on the defined model, which is fine for the purpose of learning the full-text search topic.

First, let's create the app and launch the mongo shell from the app directory:

>> meteor create search

We need to keep our app running in order to launch the shell. Open another terminal and change the directory to the root of the app, then execute the following command:

>> meteor mongo

If the app is running, it will return the MongoDB version and the connection to the database instance:

MongoDB shell version: 3.2.6

connecting to: 127.0.0.1:3001/meteor

The next step of importing the data is to save it as a JSON file on the disk. We can use the same name as that of the data from the primer-dataset.json link. Copy and paste the data from the link: https://raw.githubusercontent.com/mongodb/docs-assets/primer-dataset/primer-dataset.json into the file. Importing data with the MongoDB import tool is a straightforward task. Note that you need to execute it outside the Meteor mongo shell. Launch another terminal and execute the command:

mongoimport -h localhost:3001 —db meteor —collection restaurants —type json —file ~/chapter_six/search/primer-dataset.json

The first argument (-h) is the hostname of the database where we need to connect (localhost:3001). The database's name is meteor (the default name when we created the app). We then specify where we want to import the data (the restaurants collection); the data type is JSON and the last argument is the path of the file. If you have never had MongoDB installed outside a Meteor project, you may get an error if you run the tool. To fix this, you need to install the mongo-clients package:

>>sudo apt install



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.