<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=Python%2FMongoDB</id>
	<title>Python/MongoDB - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://aznot.com/index.php?action=history&amp;feed=atom&amp;title=Python%2FMongoDB"/>
	<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Python/MongoDB&amp;action=history"/>
	<updated>2026-05-04T17:47:37Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.0</generator>
	<entry>
		<id>https://aznot.com/index.php?title=Python/MongoDB&amp;diff=7658&amp;oldid=prev</id>
		<title>Kenneth at 21:38, 20 July 2024</title>
		<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Python/MongoDB&amp;diff=7658&amp;oldid=prev"/>
		<updated>2024-07-20T21:38:10Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table style=&quot;background-color: #fff; color: #202122;&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #202122; text-align: center;&quot;&gt;Revision as of 21:38, 20 July 2024&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l14&quot;&gt;Line 14:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 14:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;with open(&amp;#039;../files/mongo.password&amp;#039;, &amp;#039;r&amp;#039;) as file:&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;with open(&amp;#039;../files/mongo.password&amp;#039;, &amp;#039;r&amp;#039;) as file:&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     password = file.read().replace(&amp;#039;\n&amp;#039;, &amp;#039;&amp;#039;)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;     password = file.read().replace(&amp;#039;\n&amp;#039;, &amp;#039;&amp;#039;)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;with open(&#039;../files/mongo.server&#039;, &#039;r&#039;) as file:&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-side-deleted&quot;&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;    server = file.read().replace(&#039;\n&#039;, &#039;&#039;)&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# pymongo.errors.InvalidURI: Username and password must be escaped according to RFC 3986, use urllib.parse.quote_plus()&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;# pymongo.errors.InvalidURI: Username and password must be escaped according to RFC 3986, use urllib.parse.quote_plus()&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l19&quot;&gt;Line 19:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 21:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;q_password = urllib.parse.quote_plus(password)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;q_password = urllib.parse.quote_plus(password)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;creds = (f&amp;quot;{q_username}:{q_password}&amp;quot;)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;creds = (f&amp;quot;{q_username}:{q_password}&amp;quot;)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;−&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;db_url = f&quot;mongodb://{creds}@&lt;del style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;10.249.113.13&lt;/del&gt;:27017/&quot;&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot; data-marker=&quot;+&quot;&gt;&lt;/td&gt;&lt;td style=&quot;color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;db_url = f&quot;mongodb://{creds}@&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;{server}&lt;/ins&gt;:27017/&quot;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print(db_url)&lt;/div&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;print(db_url)&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;td class=&quot;diff-marker&quot;&gt;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;br&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;!-- diff cache key aznot:diff:1.41:old-7657:rev-7658:php=table --&gt;
&lt;/table&gt;</summary>
		<author><name>Kenneth</name></author>
	</entry>
	<entry>
		<id>https://aznot.com/index.php?title=Python/MongoDB&amp;diff=7657&amp;oldid=prev</id>
		<title>Kenneth: Created page with &quot;== Test ==  &lt;pre&gt; #!/usr/bin/env python3  # python -m pip install pymongo import pymongo import urllib.parse  # pymongo tutorial - https://www.w3schools.com/python/python_mongodb_getstarted.asp  with open(&#039;../files/mongo.username&#039;, &#039;r&#039;) as file:     username = file.read().replace(&#039;\n&#039;, &#039;&#039;) with open(&#039;../files/mongo.password&#039;, &#039;r&#039;) as file:     password = file.read().replace(&#039;\n&#039;, &#039;&#039;)  # pymongo.errors.InvalidURI: Username and password must be escaped according to RFC 398...&quot;</title>
		<link rel="alternate" type="text/html" href="https://aznot.com/index.php?title=Python/MongoDB&amp;diff=7657&amp;oldid=prev"/>
		<updated>2024-07-20T21:37:33Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;== Test ==  &amp;lt;pre&amp;gt; #!/usr/bin/env python3  # python -m pip install pymongo import pymongo import urllib.parse  # pymongo tutorial - https://www.w3schools.com/python/python_mongodb_getstarted.asp  with open(&amp;#039;../files/mongo.username&amp;#039;, &amp;#039;r&amp;#039;) as file:     username = file.read().replace(&amp;#039;\n&amp;#039;, &amp;#039;&amp;#039;) with open(&amp;#039;../files/mongo.password&amp;#039;, &amp;#039;r&amp;#039;) as file:     password = file.read().replace(&amp;#039;\n&amp;#039;, &amp;#039;&amp;#039;)  # pymongo.errors.InvalidURI: Username and password must be escaped according to RFC 398...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Test ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
#!/usr/bin/env python3&lt;br /&gt;
&lt;br /&gt;
# python -m pip install pymongo&lt;br /&gt;
import pymongo&lt;br /&gt;
import urllib.parse&lt;br /&gt;
&lt;br /&gt;
# pymongo tutorial - https://www.w3schools.com/python/python_mongodb_getstarted.asp&lt;br /&gt;
&lt;br /&gt;
with open(&amp;#039;../files/mongo.username&amp;#039;, &amp;#039;r&amp;#039;) as file:&lt;br /&gt;
    username = file.read().replace(&amp;#039;\n&amp;#039;, &amp;#039;&amp;#039;)&lt;br /&gt;
with open(&amp;#039;../files/mongo.password&amp;#039;, &amp;#039;r&amp;#039;) as file:&lt;br /&gt;
    password = file.read().replace(&amp;#039;\n&amp;#039;, &amp;#039;&amp;#039;)&lt;br /&gt;
&lt;br /&gt;
# pymongo.errors.InvalidURI: Username and password must be escaped according to RFC 3986, use urllib.parse.quote_plus()&lt;br /&gt;
q_username = urllib.parse.quote_plus(username)&lt;br /&gt;
q_password = urllib.parse.quote_plus(password)&lt;br /&gt;
creds = (f&amp;quot;{q_username}:{q_password}&amp;quot;)&lt;br /&gt;
db_url = f&amp;quot;mongodb://{creds}@10.249.113.13:27017/&amp;quot;&lt;br /&gt;
print(db_url)&lt;br /&gt;
&lt;br /&gt;
db_client = pymongo.MongoClient(db_url)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;List Databases:&amp;quot;)&lt;br /&gt;
print(db_client.list_database_names())&lt;br /&gt;
&lt;br /&gt;
# MongoDB will create the database if it does not exist, and make a connection to it.&lt;br /&gt;
db = db_client[&amp;quot;test&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
# MongoDB will create the collection if it does not exist.&lt;br /&gt;
people = db[&amp;quot;people&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
person = {&amp;quot;name&amp;quot;: &amp;quot;John&amp;quot;, &amp;quot;color&amp;quot;: &amp;quot;blue&amp;quot;}&lt;br /&gt;
x = people.insert_one(person)&lt;br /&gt;
print(x.inserted_id)&lt;br /&gt;
&lt;br /&gt;
person = {&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;, &amp;quot;color&amp;quot;: &amp;quot;Green&amp;quot;}&lt;br /&gt;
x = people.insert_one(person)&lt;br /&gt;
print(x.inserted_id)&lt;br /&gt;
&lt;br /&gt;
# Find first person in collection&lt;br /&gt;
first_person = people.find_one()&lt;br /&gt;
print(&amp;quot;First person:&amp;quot;)&lt;br /&gt;
print(first_person)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Find people who like blue:&amp;quot;)&lt;br /&gt;
# if you want to sort:  blue_people.sort(KEY)&lt;br /&gt;
blue_people = people.find({&amp;quot;color&amp;quot;: &amp;quot;blue&amp;quot;}).sort(&amp;#039;name&amp;#039;)&lt;br /&gt;
# print(blue_people)&lt;br /&gt;
for person in blue_people:&lt;br /&gt;
    print(person)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Delete blue people&amp;quot;)&lt;br /&gt;
x = people.delete_many({&amp;quot;color&amp;quot;: &amp;quot;blue&amp;quot;})&lt;br /&gt;
print(x)&lt;br /&gt;
&lt;br /&gt;
# update one record&lt;br /&gt;
print(&amp;quot;Updating Mike:&amp;quot;)&lt;br /&gt;
x = people.update_one({&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;}, {&amp;quot;$set&amp;quot;: {&amp;quot;color&amp;quot;: &amp;quot;red&amp;quot;}})&lt;br /&gt;
print(x)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Mike:&amp;quot;)&lt;br /&gt;
mike = people.find_one({&amp;quot;name&amp;quot;: &amp;quot;Mike&amp;quot;})&lt;br /&gt;
print(mike)&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Drop collection&amp;quot;)&lt;br /&gt;
people.drop()&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Done&amp;quot;)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== keywords ==&lt;/div&gt;</summary>
		<author><name>Kenneth</name></author>
	</entry>
</feed>