当前位置: 移动技术网 > IT编程>数据库>MongoDB > Mongodb学习总结

Mongodb学习总结

2020年04月03日  | 移动技术网IT编程  | 我要评论

2020/4/2
mongodb使用的是类似与json字符串的形式存储数据
[
{
key:value
},
{
key:value
},
]

mongodb使用了不存在的对象,即创建该对象

use db 使用db数据库
show dbs 查看当前服务器中写在磁盘上的数据库
show tables 查看数据库中的collection
db 查看当前使用的数据库

1.增删改查:
增:
db.collection.insert({数据}) 自动生成 _id : objectid("")
官方推荐:
db.collection.insertone({数据}) 插入一条数据
db.collection.insertmany([{数据},{数据}]) 插入多条数据
查:
db.collection.find({条件})
db.collection.findone({条件})
改:
db.collection.update({条件},{$修改器:{数据}})
官方推荐:
db.collection.updateone({条件},{$修改器:{数据}}) 更新一条数据
db.collection.updatemany({条件},{$修改器:{数据}}) 更新所有数据
删:
db.collection.remove({条件})
官方推荐:
db.collection.deleteone({条件}) 删除一条数据
db.collection.deletemany({条件}) 删除所有符合条件的数据

清除collection:
db.collection.drop()

2.$关键字
数学比较符:
$lt
$lte
$gt
$gte
$eq :
db.collection.find("score":{$gt:80})
查询关键字:
$or db.collection.find({$or:[{name:1},{age:73}]})
$in db.collection.find({age:{$in:[1,2,3,4]}}) #符合其中一个条件即可
$all db.collection.find({hobby:{$all:[1,2,3,4]}}) #子集查询

2018年12月25日:
1.$修改器 :
$set 简单粗暴 {name:value} dict["name"]=value

$unset 简单粗暴的删除字段 {$unset:{name:1}} del dict["name"]
db.user_info.updateone({age:200},{$unset:{age:1}})

$inc 引用增加
db.user_info.updatemany({},{$inc:{age:1}})

array操作
$push 在array中追加一个新的元素 [].append(item)
db.user_info.updateone({name:"200wansui"},{$push:{hobby:10}})

$pull 在array中删除一个的元素 [].remove(item) [].pop(-1)
db.user_info.updateone({name:"200wansui"},{$pull:{hobby:0}})

$pop 不含索引 -1 从前往后  1 从后往前
db.user_info.updateone({name:"200wansui"},{$pop:{hobby:1}})

2.$ 字符
db.user_info.updateone({hobby:6},{$set:{"hobby.$":"六"}})
保存符合索引条件数据的下标

3.object 字典操作
db.user_info.updateone({name:"200wansui"},{$inc:{"info.tizhong":-5}})
db.user_info.updateone({name:"200wansui"},{$set:{"info.long":12.5}})

4.array + object
db.user_info.updateone({"hobby.shengao":150},{$set:{"hobby.$.long":14}})

5.limit
db.user_info.find({}).limit(5)
选取数据从当前位置选择5个

6.skip 跳过
db.user_info.find({}).skip(2)
从0开始跳过2条数据为当前位置

7.sort
db.user_info.find({}).sort({ id:-1 })
根据id进行排序 -1倒叙 1正序

8.limit+skip+sort
db.user_info.find({}).limit(5).skip(10)
db.user_info.find({}).limit(c).skip((p-1)*c)

db.user_info.find({}).limit(5).skip(5).sort({ id:-1 })

优先级最高的是 sort 
其次优先为 skip
最低优先级 limit

9.pymongo
import pymongo
from bson import objectid

mongo_client = pymongo.mongoclient(host="127.0.0.1",port=27017)
mongo = mongo_client["数据库"]

查询数据
res = list(mongo.user_info.find({}))
print(res)

res = mongo.user_info.find_one({"id":20})
res["_id"] = str(res["_id"])

res = list(mongo.user_info.find({"$or":[{"name":"dwb"},{"id":15}]}))
print(res)

objectid json操作
res_obj = mongo.user_info.find_one({"_id":objectid(res["_id"])})
print(res_obj)
print(res.get("name"),type(res.get("_id")),type(res))

import json
res_json = json.dumps(res)
print(res_json)

增加数据
res = mongo.user_info.insert_one({"name":"pymongo","age":666})
print(res,res.inserted_id)

res = mongo.user_info.insert_many([{"name":"pymongo","age":666},{"name":"pymongo","age":666}])
print(res,res.inserted_ids)

for doc in res:
print(doc)

修改数据
res = mongo.user_info.update_many({"age":666},{"$set":{"name":"pydwb","age":999}})
print(res,dir(res),res.raw_result)

删除数据
res = mongo.user_info.delete_one({"id":20})
res = mongo.user_info.delete_many({"name":1})
print(res,dir(res),res.raw_result)

skip sort limit

res = list(mongo.user_info.find({}).limit(5))
print(len(res))

res = list(mongo.user_info.find({}).limit(5).skip(5))
print(len(res),res)

res = list(mongo.user_info.find({}).sort("age",pymongo.descending))
print(res)

res = list(mongo.user_info.find({}).sort("age",pymongo.descending).skip(5).limit(2))
print(res)

python 的 update
res = mongo.user_info.find_one({"name":"200wansui"})
print(res)
res.get("info")["shengao"] = 170
res.get("info")["tizhong"] = 130
res.get("info")["long"] = 18.5

mongo.user_info.update_one({"_id":res.get("_id")},{"$set":res})
res = mongo.user_info.find_one({"name":"200wansui"})
print(res)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网