Skip to content

Instantly share code, notes, and snippets.

@merajsiddiqui
Last active March 25, 2017 02:27
Show Gist options
  • Save merajsiddiqui/7eb45e4004c8b3e6b94e89c4383a1067 to your computer and use it in GitHub Desktop.
Save merajsiddiqui/7eb45e4004c8b3e6b94e89c4383a1067 to your computer and use it in GitHub Desktop.
MongoDB installationon Linux and using with PHP
To Enter into mongo shell type in terminal
mongo
To create a new database just type
use Database_name
To see current database
db
To list all databses
show dbs
To create database user
db.createUser({
user:"meraj",
pwd:"@server",
roles: ["readWrite", "dbAdmin"]
});
//Set roles according to your need
//collection is the table
To create a new collection
db.createCollection("collection_name");
All collection list
show collection
Insert
db.collection_name.insert(json object);
Fetch
db.collection_name.find();
Pretty print
db.collection_name.find().pretty();
Update data
db.collection_name.update("match_patter", data_to_add)
Update if found else insert
db.customer.update({condition}, {$unset :{} }, {upsert : true})
remove a field
db.customer.remove({condition}, {juustOne:true})
Set Key
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
Set MongoDB folder
echo "deb http://repo.mongodb.org/apt/ubuntu trusty/mongodb-org/3.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.0.list
Install Latest stable version
sudo apt-get install -y mongodb-org
/**
*Note Only For 16.10 users
*
*You need to create a new service unit
*
*/
sudo nano /etc/systemd/system/mongodb.service
paste the below codes in this file
-- From here
[Unit]
Description=High-performance, schema-free document-oriented database
After=network.target
[Service]
User=mongodb
ExecStart=/usr/bin/mongod --quiet --config /etc/mongod.conf
[Install]
WantedBy=multi-user.target
-- To here
Setting up firewall for default port
sudo ufw allow from your_other_server_ip/32 to any port 27017
Start MongoDB
sudo service mongod start
Stop MongoDB
sudo service mongod stop
Restart MongoDB
sudo service mongod restart
Note for 16.10 users
Start MongoDB
sudo systemctl start mongodb or sudo service mongodb start
Stop MongoDB
sudo systemctl stop mongodb or sudo service mongodb stop
Restart MongoDB
sudo systemctl restart mongodb or sudo service mongodb restart
Status of MongoDb
sudo systemctl status mongodb or sudo service mongodb status
Remove MongoDB
sudo apt-get purge mongodb-org*
Remove associated folders
sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
<?php
/**
* @author Meraj Ahmad Siddiqui <merajsiddiqui@outlook.com>
* @package mongodb class to handle all type of request
*/
class MongoDB
{
private $_connection;
private $_user;
private $_password;
//Setting up new Connectio
function __construct()
{
$this->_connection = new Mongo("mongodb://{$_user}:{$_password}@localhost");
return $this->_connection;
}
public function _newCollection(){
}
public function _updateCollection(){
}
}
$mongoClient = new MongoDB();
?>
get driver
wget https://pecl.php.net/get/mongo-1.6.14.tgz
Install phpize
sudo apt get install phpize
Extract the tar
tar tar zxvf mongo-1.6.14.tgz
Go to extracted directory
cd mongo-1.6.14
Start setting up driver
phpize
./configure
make all
sudo make install
apt-get install pkg-config
pecl install mongodb
echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment