Get started with MongoDB with Python
1. Install it
https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/#install-mongodb-community-edition2. Use it
https://api.mongodb.com/python/current/tutorial.html3. For Python
```from pymongo import MongoClient
import datetime
import pprint
client = MongoClient('mongodb://localhost:27017/')
db = client.test_database
posts = db.posts
posts.delete_many({})
post = {
"author": "Mike",
"text": "My first blog post!",
"tags": ["mongodb", "python", "pymongo"],
"date": datetime.datetime.utcnow()
}
post2 = {
"author": "yingshaoxo",
"text": "My first latex book!",
"tags": ["mongodb", "python", "pymongo", "latex"],
# "date": datetime.datetime.utcnow()
}
posts.insert_one(post)
posts.insert_one(post2)
post = posts.find_one({'author': 'Mike'})
print("Find one item from the database:")
pprint.pprint(post)
print('\n'*2)
posts.find_one_and_update({"_id": post.get("_id")}, {"$set": {"new_field": "888888888888888888888888888888888888888888888"}})
print("Find multiple items from the database:")
for post in posts.find():
pprint.pprint(post)
print()
print('\n'*2)
print(client.list_database_names())
```
4. For mongo shell
```mongo
help
show dbs
use test_database
show collections
db.posts.find()
db.posts.deleteMany({"author": {"$regex": ".+"}})
```
Insert an element to a list inside of one item of a collection of mongodb:
ReplyDelete> db.test.insertOne({"name": "ys", "history": [{"date":"1", "to": "nowhere"}]})
> db.test.findOneAndUpdate({"name": "ys"}, {"$addToSet": {"history": {"date": "2", "to": "somewhere"}}})
> db.test.find()
{ "_id" : ObjectId("5e683517bb61a572d556dcd2"), "name" : "ys", "history" : [ { "date" : "1", "to" : "nowhere" }, { "date" : "2", "to" : "somewhere" } ] }