Continuing from:
https://tthtlc.wordpress.com/2020/12/26/setting-nginx-mariadb-server-php-nodejs-mongodb-application/
Basics of MongoDB and NodeJS is covered in other articles.
Now we will import a dataset in csv (named as “xxxx.csv”) file format into our MongoDB database. The “mongo” command is used to drop the collection in the database, and “mongoimport” is used to import the csv file into MongoDB.
The database name is named as “iot_traffic” and the collection is named “dataset1”:
mongo iot_traffic --eval 'db.dataset1.drop()' mongoimport --type csv -d iot_traffic -c dataset1 --headerline xxxx.csv
After import, next is to read the database and display it. Put the following content into a javascript file (for example, “mongo_list_data.js”)
const mongo = require('mongodb'); const url = "mongodb://localhost:27017"; mongo.connect(url, {useNewUrlParser: true}, (err, db) => { if(err) { console.log(err); process.exit(0); } var dbo = db.db('iot_traffic'); console.log('database connected!'); var collection = dbo.collection('dataset1'); collection.find().toArray((err, results) => { if(err) { console.log(err); process.exit(0); } console.log(results); db.close(); }); });
And then execute the javascript file via “nodejs mongo_list_data.js”):
As noted above, there is no username information needed to access the database.
But if the a user is created as follows (put this into a file and called it “create_user.sh”):
mongo <<EOF use iot_traffic db.dropUser( "iotuser" ) db.createUser( { user: "iotuser", pwd: "xxxxxx", customData: { employeeId: 12345 }, roles: [ { role: "readWrite", db: "iot_traffic" } ] }, { w: "majority" , wtimeout: 5000 } ) EOF
So effectively the “mongo” command is used to create the database user.
Then to access the database via username and password will be as follows (username is “iotuser” and password is “xxxx”), put this into a javascript file and named it as “mongo_data_list.js”)
const mongo = require('mongodb'); const url = "mongodb://iotuser:xxxx@localhost:27017/iot_traffic"; mongo.connect(url, {useNewUrlParser: true}, (err, db) => { if(err) { console.log(err); process.exit(0); } var dbo = db.db('iot_traffic'); console.log('database connected!'); var collection = dbo.collection('dataset1'); collection.find().toArray((err, results) => { if(err) { console.log(err); process.exit(0); } console.log(results); db.close(); }); });
And then to verify that the user is created, you have to be inside the “iot_traffic” database. First using “mongo” command:
And then execute the file as “nodejs mongo_data_list.js”) and the output will be the same below:
Alternatively you can use the following program (using “MongoClient”) to connect to MongoDB:
const MongoClient = require('mongodb').MongoClient; const uri = "mongodb://iotuser:xxxx@localhost:27017/iot_traffic?retryWrites=true&w=majority"; const client = new MongoClient(uri, { useNewUrlParser: true }); client.connect(err => { // creating collection const collection = client.db("iot_traffic").collection("dataset1"); // perform actions on the collection object client.close(); });
References:
https://stackoverflow.com/questions/24985684/mongodb-show-all-contents-from-all-collections: How to use “mongo” command.
http://mongodb.github.io/node-mongodb-native/api-generated/mongoclient.html: MongoClient documentation
https://stackoverflow.com/questions/24985684/mongodb-show-all-contents-from-all-collections
You must be logged in to post a comment.