pymongo 查询条件

发布时间 2023-03-31 15:59:09作者: niko5960

mongo_conn = mongo['user']

1.查询
a.查询所有:mongo_conn.find({})

b.单条记录查询:mongo_conn.find_one({'name': 'Mike'})

c.多条记录查询:

对于多条数据的查询,我们可以使用find()方法,例如在这里查找年龄为20的数据,示例如下:

results = mongo_conn.find({'age': 20}) # 返回结果是Cursor类型,相当于一个生成器,我们需要遍历取到所有的结果,每一个结果都是字典类型。

for result in results:

print(result)

d.条件规则查询

如果要查询年龄大于20的数据,则写法如下:results = collection.find({'age': {'$gt': 20}})

在这里将比较符号归纳如下表:

$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]}}

在这里将一些功能符号再归类如下:

$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'}自身粉丝数等于关注数

2.计数
要统计查询结果有多少条数据,可以调用count()方法,

a.如统计所有数据条数:

count = mongo_conn.find().count()

b. 或者统计符合某个条件的数据:

count = mongo_conn.find({'age': 20}).count()

  1. 排序
    a.可以调用sort方法,传入排序的字段及升降序标志即可,

示例如下:

results = mongo_conn.find().sort('name', pymongo.ASCENDING)

b. 偏移,可能想只取某几个元素,在这里可以利用skip()方法偏移几个位置,比如偏移2,就忽略前2个元素,得到第三个及以后的元素。

results = mongo_conn.find().sort('name', pymongo.ASCENDING).skip(2)

c. 另外还可以用limit()方法指定要取的结果个数,示例如下:

results = mongo_conn.find().sort('name', pymongo.ASCENDING).skip(2).limit(2)

值得注意的是,在数据库数量非常庞大的时候,如千万、亿级别,最好不要使用大的偏移量来查询数据,很可能会导致内存溢出,可以使用类似find({'_id': {'$gt': ObjectId('593278c815c2602678bb2b8d')}}) 这样的方法来查询,记录好上次查询的_id。

4.更新
a. 对于数据更新可以使用update()方法,指定更新的条件和更新后的数据即可,例如:

condition = {'name': 'Kevin'}

student = mongo_conn.find_one(condition)

student['age'] = 25

result = mongo_conn.update(condition, student) #运行结果: {'ok': 1, 'nModified': 1, 'n': 1, 'updatedExisting': True}

返回结果是字典形式,ok即代表执行成功,nModified代表影响的数据条数。

b 另外update()方法其实也是官方不推荐使用的方法,在这里也分了update_one()方法和update_many()方法,用法更加严格,

第二个参数需要使用$类型操作符作为字典的键名,我们用示例感受一下。

condition = {'name': 'Kevin'}

student = mongo_conn.find_one(condition)

student['age'] = 26

result = mongo_conn.update_one(condition, {'$set': student}) # 其返回结果是UpdateResult类型,然后调用matched_count和modified_count属性分别可以获得匹配的数据条数和影响的数据条数。

print(result.matched_count, result.modified_count)

我们再看一个例子:

condition = {'age': {'$gt': 20}}

result = mongo_conn.update_one(condition, {'$inc': {'age': 1}}) # 在这里我们指定查询条件为年龄大于20,然后更新条件为{'$inc': {'age': 1}},执行之后会将第一条符合条件的数据年龄加1。

如果调用update_many()方法,则会将所有符合条件的数据都更新,示例如下:

condition = {'age': {'$gt': 20}}

result = mongo_conn.update_many(condition, {'$inc': {'age': 1}}) # 所有匹配到的数据都会被更新。

c. replace_one()

student = mongo_conn.replace_one({'hp': 1}, {'tel': 1}) #更改存储键名

  1. 删除
    a. 删除操作比较简单,直接调用remove()方法指定删除的条件即可,符合条件的所有数据均会被删除,示例如下:

result = mongo_conn.remove({'name': 'Kevin'}) # 运行结果: {'ok': 1, 'n': 1}

b. delete_one()和delete_many()方法:

result = mongo_conn.delete_one({'name': 'Kevin'})

result = mongo_conn.delete_many({'age': {'$lt': 25}})

print(result.deleted_count)

delete_one()即删除第一条符合条件的数据,delete_many()即删除所有符合条件的数据,返回结果是DeleteResult类型,

可以调用deleted_count属性获取删除的数据条数。

6.组合方法
a.find_one_and_delete

result = mongo_conn.find_one_and_delete({"name":"zheng"}) # 查找第一条并删除

b.find_one_and_replace

result = mongo_conn.find_one_and_replace({'x': 1}, {'y': 1}) # 查找第一条并替换

c.find_one_and_update

result = mongo_conn。find_one_and_update({'_id': 665}, {'$inc': {'count': 1}, '$set': {'done': True}}) #运行结果: {u'_id': 665, u'done': False, u'count': 25}}

7.情况表数据

result = a.remove()
{u'ok': 1, u'n': 3} 3代表个数,1表示成功
8. 删除表

result = a.drop()
————————————————
版权声明:本文为CSDN博主「墨痕诉清风」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u012206617/article/details/93747295