当前位置:网站首页 > 技术博客 > 正文

mongodb orm python



Python Mongodb Python Mongodb

MongoDB 中使用了 find 和 find_one 方法来查询集合中的数据,它类似于 SQL 中的 SELECT 语句。

本文使用的测试数据如下:

我们可以使用 方法来查询集合中的一条数据。

查询 sites 文档中的第一条数据:

输出结果为:

{'_id': ObjectId('5b23696acf269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'}

方法可以查询集合中的所有数据,类似 SQL 中的 操作。

以下实例查找 sites 集合中的所有数据:

输出结果为:

 {'_id': ObjectId('5b23696acf269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'} {'_id': ObjectId('5b2369cacf3698a1cf'), 'name': 'Google', 'alexa': '1', 'url': 'https://www.google.com'} {'_id': ObjectId('5b236aa9cf5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'} {'_id': ObjectId('5b236aa9cf5236bbb7'), 'name': '', 'alexa': '101', 'url': 'https://www..com'} {'_id': ObjectId('5b236aa9cf5236bbb8'), 'name': 'Facebook', 'alexa': '10', 'url': 'https://www.facebook.com'} {'_id': ObjectId('5b236aa9cf5236bbb9'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com'} {'_id': ObjectId('5b236aa9cf5236bbba'), 'name': 'Github', 'alexa': '109', 'url': 'https://www.github.com'} 

我们可以使用 find() 方法来查询指定字段的数据,将要返回的字段对应值设置为 1。

输出结果为:

 {'name': 'RUNOOB', 'alexa': '10000'} {'name': 'Google', 'alexa': '1'} {'name': 'Taobao', 'alexa': '100'} {'name': '', 'alexa': '101'} {'name': 'Facebook', 'alexa': '10'} {'name': '知乎', 'alexa': '103'} {'name': 'Github', 'alexa': '109'} 

除了 _id,你不能在一个对象中同时指定 0 和 1,如果你设置了一个字段为 0,则其他都为 1,反之亦然。

以下实例除了 alexa 字段外,其他都返回:

输出结果为:

 {'_id': ObjectId('5b23696acf269f28d1'), 'name': 'RUNOOB', 'url': 'https://www.runoob.com'} {'_id': ObjectId('5b2369cacf3698a1cf'), 'name': 'Google', 'url': 'https://www.google.com'} {'_id': ObjectId('5b236aa9cf5236bbb6'), 'name': 'Taobao', 'url': 'https://www.taobao.com'} {'_id': ObjectId('5b236aa9cf5236bbb7'), 'name': '', 'url': 'https://www..com'} {'_id': ObjectId('5b236aa9cf5236bbb8'), 'name': 'Facebook', 'url': 'https://www.facebook.com'} {'_id': ObjectId('5b236aa9cf5236bbb9'), 'name': '知乎', 'url': 'https://www.zhihu.com'} {'_id': ObjectId('5b236aa9cf5236bbba'), 'name': 'Github', 'url': 'https://www.github.com'} 

以下代码同时指定了 0 和 1 则会报错

错误内容大概如下:

... pymongo.errors.OperationFailure: Projection cannot have a mix of inclusion and exclusion. ...

我们可以在 中设置参数来过滤数据。

以下实例查找 name 字段为 "RUNOOB" 的数据:

输出结果为:

{'_id': ObjectId('5b23696acf269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'}

查询的条件语句中,我们还可以使用修饰符。

以下实例用于读取 name 字段中第一个字母 ASCII 值大于 "H" 的数据,大于的修饰符条件为 :

输出结果为:

 {'_id': ObjectId('5b23696acf269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'} {'_id': ObjectId('5b236aa9cf5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'} {'_id': ObjectId('5b236aa9cf5236bbb7'), 'name': '', 'alexa': '101', 'url': 'https://www..com'} {'_id': ObjectId('5b236aa9cf5236bbb9'), 'name': '知乎', 'alexa': '103', 'url': 'https://www.zhihu.com'} 

我们还可以使用正则表达式作为修饰符。

正则表达式修饰符只用于搜索字符串的字段。

以下实例用于读取 name 字段中第一个字母为 "R" 的数据,正则表达式修饰符条件为 :

输出结果为:

{'_id': ObjectId('5b23696acf269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'}

如果我们要对查询结果设置指定条数的记录可以使用 方法,该方法只接受一个数字参数。

以下实例返回 3 条文档记录:

输出结果为:

{'_id': ObjectId('5b23696acf269f28d1'), 'name': 'RUNOOB', 'alexa': '10000', 'url': 'https://www.runoob.com'} {'_id': ObjectId('5b2369cacf3698a1cf'), 'name': 'Google', 'alexa': '1', 'url': 'https://www.google.com'} {'_id': ObjectId('5b236aa9cf5236bbb6'), 'name': 'Taobao', 'alexa': '100', 'url': 'https://www.taobao.com'}

Python Mongodb Python Mongodb

  • 上一篇: shtml和html
  • 下一篇: linux 去重复
  • 版权声明


    相关文章:

  • shtml和html2025-03-28 15:01:05
  • densenet121 pytorch2025-03-28 15:01:05
  • 性能测试一般用什么工具2025-03-28 15:01:05
  • k折交叉验证 pytorch2025-03-28 15:01:05
  • oracle游标原理2025-03-28 15:01:05
  • linux 去重复2025-03-28 15:01:05
  • 请说一说成员变量与局部变量的区别2025-03-28 15:01:05
  • vmware虚拟机桥接模式没有网2025-03-28 15:01:05
  • 找黑客有风险吗2025-03-28 15:01:05
  • densenet原理2025-03-28 15:01:05