mangodb的存储

mongodb基本命令

mongo #进入mongo命令行
show dbs #查看所有数据库 use tutorial #切换到名为tutorial的数据库 show collections #查看数据库下的所有集合,例如集合名为QuoteItem db. QuoteItem.
find() #查看集合QuoteItem下的所有数据

1. 插入数据

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)  # 连接数据库
db=client.test   #指定数据库
collection = db.students   #指定集合,相当于数据表

student1 = {
        'id': '20170102',
        'name': 'lilei',
        'age': 24,
        'gender':'female'
}

student2 = {
        'id': '20170302',
        'name': 'hong',
        'age': 20,
        'gender':'male'
}

# 插入一条数据 #result1
= collection.insert_one(student1)

# 插入多条数据 result2
= collection.insert_many([student1, student2]) #print(result1) #print(result1.inserted_id) #获得_id print(result2) print(result2.inserted_ids) #获得_id列表

注意

1. 插入一条和插入多条不能同时写入,否则会ID冲突的报错

2. 在mongoDB中,每条数据都有一个_id属性来唯一标识。如果没有显示指明该属性,mongodb会自动产生一个ObjectId类型的_id属性

2. 查询

from bson.objectid import ObjectId
import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db=client.test
collection = db.students

#find_one()查询一条数据 result1
= collection.find_one({'name': 'hong'}) result2 = collection.find_one({'_id': ObjectId('5b4ef71ea5979a113cfa8bc0')})

# find()用来查询多条数据,返回类型是cursor的一种生成器 result3
= collection.find({'age':20})
# 查询年龄大于20的数据 result4
= collection.find({'age': {'$gt': 20}})
# 查询名字以h开头的数据 result5
= collection.find({'name': {'$regex': '^h.*'}}) print(type(result1)) print(result1) print(result2) print(result3) for result in result3: print(result) for result in result4: print(result) print(" 正则匹配") for result in result5: print(result)

2.1 比较符号

符号 含义 示例
$lt 小于 {'age': {'$lt': 20}}
$gt 大于 {'age': {'$gt': 20}}
$lte 小于等于 {'age': {'$lte': 20}}
$gte 大于等于 {'age': {'$gte': 20}}
$ne 不等于 {'age': {'$ne': 20}}
$in 在范围内 {'age': {'$in': [20, 23]}}
$nin 不在范围内 {'age': {'$nin': [20, 23]}}

  

 2.2 功能符号

符号 含义 示例 示例含义
$regex 匹配正则表达式 {'name': {'$regex': '^M.*'}} name以M开头
$exists 属性是否存在 {'name': {'$exists': True}} name属性存在
$type 类型判断 {'age': {'$type': 'int'}} age的类型为int
$mod 数字模操作 {'age': {'$mod': [5, 0]}} 年龄模5余0
$text 文本查询 {'$text': {'$search': 'Mike'}} text类型的属性中包含Mike字符串
$where 高级条件查询 {'$where': 'obj.fans_count == obj.follows_count'} 自身粉丝数等于关注数

  更详细的用法可到官方文档找到:https://docs.mongodb.com/manual/reference/operator/query/

3. 计数,排序,偏移

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db=client.test
collection = db.students

# 一共有多少条数据的计数
count1 = collection.find().count()
print(count1)

# 符合某个条件的数据的计数
count2 = collection.find({'age': 20}).count()
print(count2)

#排序,按年龄升序; 降序的话用DESCENDING
result1 = collection.find().sort('age', pymongo.ASCENDING)
print([result['age'] for result in result1])


# 按年龄升序排序,从第4个数据开始输出
result2 = collection.find().sort('name', pymongo.ASCENDING).skip(3)

# 按年龄升序排序,从第4个数据开始输出,并且限制输出2条数据
result3 = collection.find().sort('name', pymongo.ASCENDING).skip(3).limit(2)

print([result['name'] for result in result2])
print([result['name'] for result in result3])

4. 更新

4.1 update_one()更新一条数据

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db=client.test
collection = db.students

condition = {'name':'hong'} #定义一个按条件查找的变量

#按条件找出数据并修改
student = collection.find_one(condition)
student['name'] = 'hong123'

#使用update_one()更新一个数据
result = collection.update_one(condition,  {'$set':student})

print(result)
#调用matched_count和modified_count属性获得匹配的数据条目和影响的数据条目
print(result.matched_count, result.modified_count)

注意:

$set的作用是只更新student字典内存在的字段,如果原先还有其他字段,则不会更新,也不会删除。

如果不用$set的话,则会把之前的数据全部用student字典替换,如果原本存在其他字段,则会被删除

 

4.2  update_many() 更新多条数据

import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db=client.test
collection = db.students

condition = {'age':20}
#condition = {'age': {'$gt': 20}} 年龄大于20的

# {'$inc': {'age': 1}}这里表示age=20的数据全部加1,也就是改为21
result = collection.update_many(condition,  {'$inc': {'age': 1}})

print(result)
print(result.matched_count, result.modified_count)

5. 删除

from bson.objectid import ObjectId
import pymongo

client = pymongo.MongoClient(host='localhost', port=27017)
db=client.test
collection = db.students


#result1 = collection.remove({'_id': ObjectId('5b4ef71ea5979a113cfa8bc0')})
#result2 = collection.delete_one({'name': 'hong'})
result3 = collection.delete_many({'age': {'$lt':25})


#print(result1)
print(result3.deleted_count)

传统的方式是用remove(),可删除符合条件的一个或者多个数据

新的方式是用delete_one() 删除一条数据,delete_many() 删除多条数据,这种方法可用deleted_count属性来获取删除的数据条目

原文地址:https://www.cnblogs.com/regit/p/9337101.html