MongoDB命令及python使用

1. MongoDB 是介于关系型数据库和非关系型数据库之间的产品,数据存在磁盘中,不是存在内存中。速度比redis慢,比MySQL快。

2. MongoDB 一般保存的数据是结构化的数据,比如JSON数据

3. 进入到MongoDB的交互环境中:mongo (cmd如果报错不是可执行的命令,则需要配置环境变量)

4. MongoDB 命令一般保存在 JavaScript文件中,MySQL命令一般保存在sql文件中,Redis命令一般保存在sh文件中

数据库——增删查

use my_database  (可以切换到不存在的数据库中,一旦插入数据,则该数据库会被创建)
db.dropDatabase()
db  (刚进入交互环境时,显示的是test,这是一个虚拟的数据库,并不存在。如果存入数据,则这个数据库会被创建)
show dbs / show databases

集合——增删查

db.createCollection("my_collection")
db.createCollection("collection_2", {capped: true, size:10})   指定集合容量大小为10字节
db.collection_2.drop()
show collections

数据——增删查改

MongoDB中主要的数据类型:
Object ID 文档ID、
String utf-8字符集、
Boolean、Integer、Double、Arrays、
Object 用于嵌入式的文档、
Null、
Timestamp 时间戳、
Date 存储当前日期或时间的UNIX时间格式
db.my_collection.insert({"name":"leo", "age":18})
db.my_collection.insert({name:'leo', "age":23})  单引号和双引号可以混用,键可以不加引号
db.my_collection.insert({_id:123123, name:'leo', "age":20})  保存数据时可以自定义_id,注意_id不可重复

db.my_collection.remove({name:"leo"}, {justOne:true})  删除出现字段{name:"leo"}的数据中的第一个
db.my_collection.remove({name:"leo"})  删除出现字段{name:"leo"}的所有数据

db.my_collection.find()           展示所有数据
db.my_collection.find().pretty()           展示所有数据,对数据进行格式化输出
db.my_collection.find({age:18})    查询符合条件的所有数据
db.my_collection.findOne({age:18})  查询符合条件的一条数据

db.my_collection.save({_id:123123, name:'leo', height:183})  可以对指定_id的数据进行更新
db.my_collection.save({_id:123124, name:'leo', height:183, home:"china"})  当不存在该_id时,插入新的数据
db.my_collection.update({home:"china"}, {interest:"唱歌"})  原数据的其他字段会遗失
db.my_collection.update({height:183}, {$set:{interest:"唱歌"}})  仅插入了interest字段,不会对原数据的其他字段造成影响
db.my_collection.update({height:183}, {$set:{height:185}})  仅更新了height字段,不会对原数据的其他字段造成影响

比较运算符:$lt 小于、$lte 小于等于、$gt 大于、$gte 大于等于、$ne 不等于
         db.my_collection.find({age:{$gt:10}})

成员运算符:$in
          db.my_collection.find({age:{$in:[10, 20]}})
逻辑运算符:$or 或
        db.my_collection.find({age:18, name:"leo"})
        db.my_collection.find({$or: [{age:18}, {name:"leo"}]})
正则表达式查询:
        db.my_collection.find({name:/^l/})  ^符号代表开头
        db.my_collection.find({name:{$regex: "o$"}})  $符号代表结尾
翻页查询:
        db.my_collection.find().limit(2)   查询前2条数据
        db.my_collection.find().skip(2)    查询所有数据并跳过前两条
        db.my_collection.find().skip(2).limit(2)    跳过前2条数据后,查询2条数据,实现翻页功能
自定义查询:
        db.my_collection.find({$where: function(){return this.age<=18}})  自定义js函数进行查询
投影:(显示指定的字段)
        db.my_collection.find({age: {$gt:18}},{name:1})  查询结果显示name字段,默认会显示_id字段
        db.my_collection.find({age: {$gt:18}},{name:1, _id:0})  查询结果只显示name字段,不显示_id字段
        db.my_collection.find({},{name:1, _id:0, age:1})  显示所有数据,只显示name、age字段,不显示_id字段
排序:
        db.my_collection.find().sort({age:-1})  根据age字段排序,逆序
        db.my_collection.find().sort({age:-1, _id:-1})  多条件排序,根据age逆序、_id逆序
统计:
        db.my_collection.find().count()  统计数据量
        db.my_collection.find({age:{$gt:18}}).count()
        db.my_collection.count()      省略find()也可以
        db.my_collection.count({age:{$gt:18}})
去重:
        db.my_collection.distinct("age")   查询age字段的去重数据
        db.my_collection.distinct("age", {age:{$gt:20}})   筛选数据后,查询age字段的去重数据

聚合管道操作:
        使用通式:db.集合名称.aggregate({管道1:{表达式1}, 管道2:{表达式2}...})
        
        常用管道
        $group: 将集合中的文档分组, 可用于统计结果
        $match: 过滤数据, 只输出符合条件的文档
        $project: 修改输入文档的结构, 如重命名、增加、删除字段、创建计算结果
        $sort: 将输入文档排序后输出
        $limit: 限制聚合管道返回的文档数
        $skip: 跳过指定数量的文档, 并返回余下的文档

        表达式
        $sum: 计算总和, $sum:1 表示以⼀倍计数
        $avg: 计算平均值
        $min: 获取最小值
        $max: 获取最大值

        // $group对应的字典中有几个键,结果中就有几个键,分组依据需要放在_id后面,取不同的字段需要在字段前加$
        db.stu_info.aggregate( { $group: {_id: "$gender", count: {$sum: 1}, avg_age: {$avg: "$age"}} } )

        // 将集合中所有的文档分为一组
        db.stu_info.aggregate( { $group: {_id: null, counter: {$sum: 1}, avg_age: {$avg: "$age"}} } )

        // 类似于投影
        db.stu_info.aggregate( { $project: {_id: 0, name: 1, age: 1} } )

        // 查询性别数据、人数统计、平均年龄,并以中文显示
        db.stu_info.aggregate(
        { $group: {_id: "$gender", count: {$sum: 1}, avg_age: {$avg: "$age"}} },
        { $project: {"性别": "$_id", "人数统计": "$count", "平均年龄": "$avg_age", _id: 0} } )

        // 拓展:查询年龄大于20或者归属地在蒙古或大理的人数
        db.stu_info.aggregate(
        { $match: { $or: [ { age: {$gt: 20} }, { hometown: {$in: ["蒙古", "大理"]} } ] }, },
        { $group: {_id: "$gender", counter: {$sum: 1}} },
        { $project: {"性别": "$_id", "统计人数": "$counter", _id: 0} } )

        // 查询男生人数、女生人数。按人数降序
        db.stu_info.aggregate( { $group: {_id: "$gender", counter: {$sum: 1}} }, { $sort: {counter: -1} } )

        // 取出当前从第一位学生之后的一条学生信息
        db.stu_info.aggregate({$skip: 1}, {$limit: 1})

python使用

# 安装第三方库pip install pymongo
from pymongo import MongoClient

# 1. 创建MongoDB连接对象
client = MongoClient(host='127.0.0.1', port=27017)

# 2. 指定集合collection
collection = client['test']['test_data']

# 3. 插入数据
res = collection.insert_one({"name":"leo","age":29})  # 插入一条数据
print(res)   # <pymongo.results.InsertOneResult object at 0x00000281D1BEA280>
data_list = [{"name":f"leo_{i}"} for i in range(10)]
res = collection.insert_many(data_list)  # 传list,插入多条数据
print(res)   # <pymongo.results.InsertManyResult object at 0x00000281D1C358E0>

# 4. 查询数据
res = collection.find_one({"name":"leo"})  # 查询一条记录
print(f"{res=}")   # 返回的是一条数据,不需要遍历

res = collection.find({"name":{"$regex":"^l"}})  # 查询所有记录,条件是name字段以l开头
print(res)  # 返回游标对象<pymongo.cursor.Cursor object at 0x0000029F6470C280>,需要进行遍历才能看到数据
for i in res:  # 通过遍历进行查询
    print(i)
# print(list(res))

# 5. 更新数据
collection.update_one({"name":"leo"},{"$set":{"name":"lucy"}})  # 更新一条数据
collection.update_many({"name":"leo"},{"$set":{"name":"lucy"}})  # 更新所有数据

# 6. 删除数据
collection.delete_one({"name":"leo_9"})  # 删除一条数据
collection.delete_many({"name":"leo_9"})  # 删除所有数据
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值