Get the App
SLTechnology News&Howtos  ›  Database  › 

Common operations of MongoDB

Shulou Source: shulou.com Published: 2022-06-01 02:14:46 10月28日 Update

Comparison between relational database nouns and MongoDB:

Relational database MongoDBDatabaseDatabaseTableCollectionRowDocumentIndexIndexJoinLookupForeign KeyReferenceMulti-table transactionSingle document transaction

Use MongoDB on the command line

Insert your first data

> show databases

Local 0.000GB

> use test # switch to test database. If not, create a new one.

Switched to db test

> show databases local 0.000GB

> db.demo.insert ({"key": "value"}) WriteResult ({"nInserted": 1})

> show databases local 0.000GB test 0.000GB

> show collections demo

> db.demo.findOne () {"_ id": ObjectId ("573af7085ee4be80385332a6"), "key": "value"}

Using MongoDB in python

Import pymongo## client defaults to localhost and port 27017. Eg MongoClient ('localhost', 27017) client = pymongo.MongoClient () # Connect to the local database blogDatabase = client ["blog"] # switch to blog database usersCollection = blogDatabase ["users"] # switch to usersCollectionusersCollection.insert_one ({"username": "jdrumgoole", "password": "top secret" "lang": "EN"}) # insert a data user = usersCollection.find_one () # find the latest data print (user) articlesCollection = blogDatabase ["articles"] author = "jdrumgoole" article = {"title": "This is my first post", "body": "The is the longer body text for my blog post. We can add lots of text here. "," author ": author," tags ": [" joe "," general "," Ireland "," admin "]} # # Lets check if our author exists#if usersCollection.find_one ({" username ": author}): articlesCollection.insert_one (article) else:raise ValueError (" Author% s does not exist "% author)

Tags: Data database switch noun command commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology Xiaomi MariaDB macOS