溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

MongoDB常用操作

發(fā)布時間:2020-10-25 09:40:35 來源:網(wǎng)絡 閱讀:343 作者:hiubuntu 欄目:數(shù)據(jù)庫

關系型數(shù)據(jù)庫名詞與MongoDB對比:

關系數(shù)據(jù)庫 MongoDB 
Database  Database
Table Collection
Row Document
Index Index
Join Lookup
 Foreign KeyReference
Multi-table transaction Single document transaction


命令行使用MongoDB

插入你的第一數(shù)據(jù) 

> show databases 

local 0.000GB 

> use test   #切換到test數(shù)據(jù)庫,如果沒有則新建

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


python中使用MongoDB

import pymongo
#
# client defaults to localhost and port 27017. eg MongoClient('localhost', 27017)

client = pymongo.MongoClient()
#連接到本地數(shù)據(jù)庫
blogDatabase = client[ "blog" ]    
 #切換到blog數(shù)據(jù)庫
usersCollection = blogDatabase[ "users" ]   
 #切換到usersCollection
usersCollection.insert_one( { "username" : "jdrumgoole",
"password" : "top secret",
"lang" : "EN" })
#插入一條數(shù)據(jù)
user = usersCollection.find_one()
#查找最新的一條數(shù)據(jù)
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 )


向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI