Lab Notes - Mongodb
Lab Notes - Mongodb
repo file:
[mongodb-org-3.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/$releasever/mongodb-org/3.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-3.4.asc
---------------------------
PYMONGO
---------------------------
post_data = {
'title': 'Python and MongoDB',
'content': 'PyMongo is fun, you guys',
'author': 'Scott'
}
result = posts.insert_one(post_data) insert one document into a collection
media = db.media
media_1 = {
"Type": "CD",
"Artist": "Nirvana",
"Title": "Nevermind",
"Genre": "Grunge",
"Releasedate": "1991.09.24",
"Tracklist": [
{
"Track" : "1",
"Title" : "Smells Like Teen Spirit",
"Length" : "5:02"
},
{
"Track" : "2",
"Title" : "In Bloom",
"Length" : "4:15"
}
]
}
media_2 = {
"type": "Book",
"Title": "Definitive Guide to MongoDB: A complete guide to dealing with Big
Data using MongoDB 2nd , The",
"ISBN": "987-1-4302-5821-6",
"Publisher": "Apress",
"Author": [
"Hows, David"
"Plugge, Eelco",
"Membrey, Peter",
"Hawkins, Tim"
]
}
Mongo shell:
Mongo shell:
---------------------
MONGOENGINE
---------------------
class Post(Document):
title = StringField(required=True, max_length=200)
content = StringField(required=True)
author = StringField(required=True, max_length=50)
published = DateTimeField(default=datetime.datetime.now)
post_1 = Post(
title='Sample Post',
content='Some engaging content',
author='Scott'
)
> db.post.find()
{ "_id" : ObjectId("59787de31ed75a450876ed42"), "title" : "A Better Post Title",
"content" : "Some engaging content", "author" : "Scott", "published" :
ISODate("2017-07-26T04:32:39.983Z") }