温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MongoDB常用操作

发布时间:2020-10-25 09:40:35 来源:网络 阅读:336 作者:hiubuntu 栏目:数据库

关系型数据库名词与MongoDB对比:

关系数据库 MongoDB 
Database  Database
Table Collection
Row Document
Index Index
Join Lookup
 Foreign KeyReference
Multi-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问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI