-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
123 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
); | ||
}) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.