加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

MongoDB的安装与python操作MongoDB

发布时间:2020-12-17 00:17:50 所属栏目:Python 来源:网络整理
导读:div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=10182348"gt; h1 id="-mongodb" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid

<div class="markdown-here-wrapper" data-md-url="https://i.cnblogs.com/EditPosts.aspx?postid=10182348"&gt;
<h1 id="-mongodb" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 24px; border-bottom: 2px solid #aaaaaa;">一.安装MongoDB
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">因为我个人使用的是windows,就只记录下windows下的安装


<h3 id="1-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 20px; border-bottom: 1px solid #aaaaaa;">1.下载安装
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">就是官网,下载msi,选个路径安装


<h3 id="2-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 20px; border-bottom: 1px solid #aaaaaa;">2.配置
<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">看见别的地方说需要手动在bin同级目录创建data和log文件夹,我安装时它们是自动生成的在data文件夹中建个专门存数据的文件夹db,在log文件夹中建个mongodb.log文件来保存日志然后以管理员身份启动cmdcd到MongoDB的bin目录下在命令行输入

 --logpath  --logappend --dbpath  --port  --serviceName  --serviceDisplayName  --install


mongod.exe —config mongod.conf执行完后会什么也没显示,那没准也是成功了。。。可是我又去系统服务里看了下。。。没有服务我想刚才尝试第一种方案时不是有了服务嘛,我又按照之前输入的命令并按照现在的配置又试了一下,就下边这个命令,注意要以管理员身份执行

 --logpath  --logappend --dbpath  --port  --serviceName  --serviceDisplayName  --install

 pymongo

client=pymongo.MongoClient(<span class="hljs-string" style="color: #d1f1a9;">'mongodb://用户名:密码@服务器IP或域名:端口号')


<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">如果没有设置权限验证,并且数据库在本机那可以像下边这样

)

,port=)

]

]
collection2=db1.test2_collection

:,:,:}
result=collection1.insert(data)
print(result)

:,:},{:,:,:}]
result=collection1.insert(data)
print(result)

:}
result=collection1.insert_one(data)
print(result)
print(result.inserted_id)

:}]
result=collection1.insert_many(data)
print(result)
print(result.inserted_ids)

:},{:,:})
print(result)

 bson.objectid  ObjectId

result=collection1.find_one({<span class="hljs-string" style="color: #d1f1a9;">'_id':ObjectId(<span class="hljs-string" style="color: #d1f1a9;">'5c23a4a8080b33197c17cb0e')})
print(result)<span class="hljs-comment" style="color: #7285b7;"># {'_id': ObjectId('5c23a4a8080b33197c17cb0e'),'name': 'sfencs','age': 20,'hobby': 'coding'}


<h5 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 16px; border-bottom: 0.5px solid #aaaaaa;">查询多条数据

<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">使用find方法

:})
print(result)
 i  result:
    print(i)
{'_id': ObjectId('5c239d86080b334ef0cd5773'),'hobby': 'coding'}
{'_id': ObjectId('5c239eaa080b3308b8224413'),'hobby': 'coding'}
{'_id': ObjectId('5c23a135080b3347b8a0038c'),'hobby': 'coding'}
{'_id': ObjectId('5c23a210080b331d546543ca'),'hobby': 'coding'}
{'_id': ObjectId('5c23a3eb080b334cbc693608'),'hobby': 'coding'}
{'_id': ObjectId('5c23a406080b33164cb4486c'),'hobby': 'coding'}
{'_id': ObjectId('5c23a4a8080b33197c17cb0e'),'hobby': 'coding'}

'''


<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">返回的是Cursor类型,相当于一个生成器,每一个结果都是一个字典


<h5 id="-" style="margin: 20px 0px 10px; padding: 0px; font-weight: bold; color: black; font-size: 16px; border-bottom: 0.5px solid #aaaaaa;">逻辑查询条件


<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">像刚才的只是固定的条件,还有一些更多可选条件

:{:}}                 | 年龄小于                  |
 $gt      | {:{:}}                 | 年龄大于                  |
 $lte      | {:{:}}                | 小于等于                    |
 $gte      | {:{:}}                | 大于等于                    |
 $ne      | {:{:}}                 | 不等于                      |
 $      | {:{:[,]}}            | 在范围内                    |
 $nin      | {:{:[,]}}           | 不在范围                    |
 $regex     | {:{:}}         | 正则表达式                  |
 $exist     | {:{:}}           | name属性存在                |
 $type      | {:{:}}            | age类型为int                |
 $mod       | {:{:[,]}}             | age除以                 |
 $text      | {:{:}}        | text类型的属性中包含aaa     |
 $where     | {:}     | 高级查询                    |

:}).sort(,-)

:}).count()
print(result)

:}).skip()

<span class="hljs-keyword" style="color: #ebbbff;">for i <span class="hljs-keyword" style="color: #ebbbff;">in result:
print(i)


<p style="margin: 0px 0px 1.2em !important; font-size: 16px; line-height: 1.75em; padding-right: 0.5em; padding-left: 0.5em;">上边这样就会跳过前三个元素我们也可以限定出来的元素的个数

:}).skip().limit()

:})
people[]=
result=collection1.update_one({:},{:people})
print(result)
print(result.matched_count,result.modified_count)

:},{:{:}})
print(result)
print(result.matched_count,result.modified_count)

:},result.modified_count)

:{:}})
print(result)
print(result.matched_count,7

:})
print(result)

:})
print(result.deleted_count)

:})
print(result.deleted_count)

)
print(result)


(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读