Skip to content

Commit

Permalink
Add files for 2, 3, 4, 6, and 7
Browse files Browse the repository at this point in the history
  • Loading branch information
npentrel committed Mar 21, 2022
1 parent 1f0b929 commit 7247bb1
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 0 deletions.
5 changes: 5 additions & 0 deletions 7/7_2.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
python3 -m pip install --user virtualenv
python3 -m venv env
source env/bin/activate
pip3 install pymongo python-dotenv
python3 main.py
7 changes: 7 additions & 0 deletions 7/7_3.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pecl install mongodb
php --ini
echo "extension=mongodb.so" >> /usr/local/etc/php/8.1/php.ini
composer --version
composer require mongodb/mongodb
composer require vlucas/phpdotenv
php index.php
7 changes: 7 additions & 0 deletions 7/7_4.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pecl install mongodb
php --ini
echo "extension=mongodb.so" >> /usr/local/etc/php/8.1/php.ini
composer --version
composer require mongodb/mongodb
composer require vlucas/phpdotenv
php index.php
45 changes: 45 additions & 0 deletions 7/nodejs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const mongoose = require('mongoose');
const { config } = require('dotenv');

main().catch(err => console.log(err));

async function main() {
config();

const uri = 'mongodb://0.0.0.0:27017/admin?retryWrites=true&w=majority';

await mongoose.connect(uri, {
dbName: "sample_data",
user: process.env.DB_USER,
pass: process.env.DB_PASSWORD
}).then(() => {
console.log('Connection established with MongoDB');
}).catch(error => console.error(error.message));

const { Schema } = mongoose;

const authorSchema = new Schema({
author: String,
message: String
}, { db: 'sample_data', collection: 'testnodejs' });

const Author = mongoose.model('Author', authorSchema);
const author = new Author({ author: 'Naomi Pentrel', message: 'hello world!' });

await author.save((error) => {
if (error) {
return console.log(`Error has occurred: ${error}`);
}
console.log('Document is successfully saved.');
Author.find(
{},
function (error, documents) {
if (error) {
return console.log(`Error has occurred: ${error}`);
}
console.log("document found:" + documents);
}
);
})
}

15 changes: 15 additions & 0 deletions 7/nodejs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "mongodb-sample-code",
"version": "1.0.0",
"description": "Sample code to connect to MongoDB",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"author": "Naomi Pentrel",
"license": "ISC",
"dependencies": {
"dotenv": "^16.0.0",
"mongoose": "^6.2.7"
}
}
23 changes: 23 additions & 0 deletions 7/php/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

require_once __DIR__ . '/vendor/autoload.php';

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();

$client = new MongoDB\Client(
'mongodb://' . $_ENV['MDB_USER'] . ':' . $_ENV['MDB_PASSWORD'] . '@localhost:27017/admin?retryWrites=true&w=majority'
);

$collection = $client->selectCollection('sample_data', 'testphp');

$document = $collection->insertOne([
'author' => 'Naomi',
'message' => 'hello world!',
]);

$document = $collection->findOne();

var_dump($document);

?>
21 changes: 21 additions & 0 deletions 7/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import os
from pymongo import MongoClient
from dotenv import load_dotenv

load_dotenv()
client = MongoClient(
'mongodb://localhost:27017/admin?retryWrites=true&w=majority',
username=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD')
)

db = client['sample_data']
collection = db['testpython']

doc = { "author": "Naomi", "message": "hello world!"}

doc_id = collection.insert_one(doc).inserted_id
print(doc_id)

found = collection.find_one()
print(found)
Empty file removed 7/script.sh
Empty file.

0 comments on commit 7247bb1

Please sign in to comment.