O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

To read data from MongoDB, follow these steps:

  1. Load the Chicago crime dataset with a data table. This dataset is taken from Kaggle and modified for this recipe:
> library(data.table)> crimes=data.table::fread("Chicago_Crimes_2012-2017.csv")
  1. Load rmongodb and mongolite:
> library(rmongodb)> library(mongolite)
  1. Create the MongoDB crime collection, and insert data into it:
> crime_collection = mongo(collection = "crimes", db = "Chicago") > crime_collection$insert(crimes)
  1. Verify the MongoDB crime collection: 
> # Using mongolite> crime_collection$count()> crime_collection$iterate()$one()> using rmongodb> mongo <- mongo.create(host = "localhost")> mongo.is.connected(mongo)> mongo.get.database.collections(mongo,"Chicago") ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required