MongoDB is a document database, which means it stores data in JSON-like documents. We believe this is the most natural way to think about data.
Mongodb installation - source code
代码语言:shell复制# mongodb installation
# https://www.mongodb.com/download-center#community
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel62-4.2.3.tgz
tar zxvf mongodb-linux-x86_64-rhel62-4.2.3.tgz
# start
mkdir -p /data/mongodb
cd /usr/local/mongodb/bin
./mongod --port 27017 --dbpath=/data/mongodb
net start MongoDB
# check
./mongod --port 27017 --dbpath=/data/mongodb
./mongo -> use admin/XXX
# Python test
# yum install -y mongodb-server
pip install pymongo
MongoDB CRUD test - python
代码语言:python代码运行次数:0复制import pymongo
# connect
mclient = pymongo.MongoClient("mongodb://10.170.15.54:27017/")
print(mclient.list_database_names())
# create
test_db = mclient["helloworld"]
test_col = test_db["sites"]
x = test_col.insert_one({"name":"ELK","type":"NOSQL","url":"https://www.elkgroupinternational.com/elk-home"})
x = test_col.insert_one({"name":"MYSQL","type":"RELATION","url":"https://www.mysql.com"})
# update
test_query = { "name": "ELK" }
newvalues = {"$set":{"name":"MONGODB","type":"NOSQL","url":"https://www.mongodb.com"}}
test_col.update_one(test_query, newvalues)
# delete
test_query = { "name": "MYSQL" }
test_col.delete_one(test_query)
# retrive
print(test_col.find_one(),test_col.find())
installation
代码语言:javascript复制# download and installation
sudo apt-get install mongodb
# databse version
mongo -version
service
代码语言:javascript复制service mongodb start
service mongodb stop
configuration
代码语言:shell复制/etc/mongodb.conf
# bind_ip = 0.0.0.0
# port = 27017
# auth = true
initialization
代码语言:javascript复制use hello_django
# insert one record
db.hello_django.insert({"question_text":"hello_django"})
show dbs
pip installation
代码语言:shell复制# pymongo provide multi connections
pip install pymongo
# Django and MongoDB connection
pip install djongo
django settings
代码语言:javascript复制# setting.py
DATABASES = {
'default': {
'ENGINE': 'djongo',
'ENFORCE_SCHEMA': True,
'NAME': 'hello_django',
'CLIENT': {
'host': '127.0.0.1',
}
}
}
operations
代码语言:shell复制# entry
mongo
# status
db.stats()
# databases
show dbs
# switch
use hello_django
# tables
show tables
# select * from polls_question
db.polls_question.find()
# exit
Done