共计 1390 个字符,预计需要花费 4 分钟才能阅读完成。
自动写代码机器人,免费开通
关系型数据库名词与 MongoDB 对比:
关系数据库 MongoDB Database DatabaseTable CollectionRow DocumentIndex IndexJoin Lookup Foreign KeyReferenceMulti-table transaction Single document transaction
命令行使用 MongoDB
插入你的第一数据
show databases
local 0.000GB
use test #切换到 test 数据库,如果没有则新建
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()
#连接到本地数据库
blogDatabase = client[ "blog" ]
# 切换到 blog 数据库
usersCollection = blogDatabase[ "users" ]
# 切换到 usersCollection
usersCollection.insert_one( { "username" : "jdrumgoole",
"password" : "top secret",
"lang" : "EN" })
#插入一条数据
user = usersCollection.find_one()
#查找最新的一条数据
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 问一下细节
丸趣 TV 网 – 提供最优质的资源集合!
正文完