0% found this document useful (0 votes)
977 views

Lab Notes - Mongodb

1. The document describes how to install and configure MongoDB on a Linux server. It creates a MongoDB repository configuration file, installs MongoDB using yum, starts the mongod service, and lists the MongoDB data directory and log file. 2. It then provides examples of using PyMongo and MongoEngine Python libraries to connect to MongoDB from Python and perform basic CRUD operations like inserting, finding, and updating documents.

Uploaded by

bdog2k
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
977 views

Lab Notes - Mongodb

1. The document describes how to install and configure MongoDB on a Linux server. It creates a MongoDB repository configuration file, installs MongoDB using yum, starts the mongod service, and lists the MongoDB data directory and log file. 2. It then provides examples of using PyMongo and MongoEngine Python libraries to connect to MongoDB from Python and perform basic CRUD operations like inserting, finding, and updating documents.

Uploaded by

bdog2k
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Create a /etc/yum.repos.d/mongodb-org-3.4.

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

yum install mongodb-org

[root@mongo ~]# ll /var/lib/mongo/


total 60
-rw-r--r--. 1 mongod mongod 4096 Jul 26 02:49 collection-0-6798417894170838295.wt
-rw-r--r--. 1 mongod mongod 4096 Jul 26 02:49 collection-2-6798417894170838295.wt
drwxr-xr-x. 2 mongod mongod 69 Jul 26 02:50 diagnostic.data
-rw-r--r--. 1 mongod mongod 4096 Jul 26 02:49 index-1-6798417894170838295.wt
-rw-r--r--. 1 mongod mongod 4096 Jul 26 02:49 index-3-6798417894170838295.wt
-rw-r--r--. 1 mongod mongod 4096 Jul 26 02:49 index-4-6798417894170838295.wt
drwxr-xr-x. 2 mongod mongod 4096 Jul 26 02:49 journal
-rw-r--r--. 1 mongod mongod 4096 Jul 26 02:49 _mdb_catalog.wt
-rw-r--r--. 1 mongod mongod 5 Jul 26 02:49 mongod.lock
-rw-r--r--. 1 mongod mongod 4096 Jul 26 02:49 sizeStorer.wt
-rw-r--r--. 1 mongod mongod 95 Jul 26 02:49 storage.bson
-rw-r--r--. 1 mongod mongod 49 Jul 26 02:49 WiredTiger
-rw-r--r--. 1 mongod mongod 4096 Jul 26 02:49 WiredTigerLAS.wt
-rw-r--r--. 1 mongod mongod 21 Jul 26 02:49 WiredTiger.lock
-rw-r--r--. 1 mongod mongod 842 Jul 26 02:49 WiredTiger.turtle
-rw-r--r--. 1 mongod mongod 4096 Jul 26 02:49 WiredTiger.wt

sudo chkconfig mongod on


sudo service mongod start

[root@mongo mongo]# cat /var/log/mongodb/mongod.log


2017-07-26T02:49:58.723-0700 I NETWORK [thread1] waiting for connections on port
27017

---------------------------
PYMONGO
---------------------------

from pymongo import MongoClient


client = MongoClient() creates a connection to localhost on port 27017
db = client.pymongo_test creates a db called pymongo_test
posts = db.posts creates a collection called posts

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"
]
}

media_insert = media.insert_many(media_1,media_2) insert documents into


collection

books = media.find({'type': 'Book'})


print(books)
<pymongo.cursor.Cursor object at 0x2392810>
this instead:
>>> for book in books:
... print(book)
...
{u'Publisher': u'Apress', u'ISBN': u'987-1-4302-5821-6', u'Title': u'Definitive
Guide to MongoDB: A complete guide to dealing with Big Data using MongoDB 2nd ,
The', u'Author': [u'Hows, DavidPlugge, Eelco', u'Membrey, Peter', u'Hawkins, Tim'],
u'_id': ObjectId('597875111ed75a292ca62af3'), u'type': u'Book'}

Mongo shell:

> db.media.find({ "type" : "Book" })


{ "_id" : ObjectId("597875111ed75a292ca62af3"), "Publisher" : "Apress", "ISBN" :
"987-1-4302-5821-6", "Author" : [ "Hows, DavidPlugge, Eelco", "Membrey, Peter",
"Hawkins, Tim" ], "Title" : "Definitive Guide to MongoDB: A complete guide to
dealing with Big Data using MongoDB 2nd , The", "type" : "Book" }

Mongo shell:

> use pymongo_test


> db.posts.find ()
{ "_id" : ObjectId("59786bf31ed75a292ca62aee"), "content" : "PyMongo is fun, you
guys", "author" : "Scott", "title" : "Python and MongoDB" }
{ "_id" : ObjectId("59786de31ed75a292ca62aef"), "content" : "PyMongo is fun, you
guys", "author" : "Scott", "title" : "Python and MongoDB" }
{ "_id" : ObjectId("59786de31ed75a292ca62af0"), "content" : "Use virtual
environments, you guys", "author" : "Scott", "title" : "Virtual Environments" }
{ "_id" : ObjectId("59786de31ed75a292ca62af1"), "content" : "Learn Python, it is
easy", "author" : "Bill", "title" : "Learning Python" }

> db.posts.find ( { "title" : "Python and MongoDB" } )


{ "_id" : ObjectId("59786bf31ed75a292ca62aee"), "content" : "PyMongo is fun, you
guys", "author" : "Scott", "title" : "Python and MongoDB" }
{ "_id" : ObjectId("59786de31ed75a292ca62aef"), "content" : "PyMongo is fun, you
guys", "author" : "Scott", "title" : "Python and MongoDB" }

---------------------
MONGOENGINE
---------------------

from mongoengine import *


connect('mongoengine_test', host='localhost', port=27017)
import datetime

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'
)

post_1.save() # This will perform an insert


print(post_1.title)
post_1.title = 'A Better Post Title'
post_1.save() # This will perform an atomic edit on "title"
print(post_1.title)

> 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") }

You might also like