一、【连接mongo服务】、【连接数据库】、【连接集合】

#一.【连接Mongo】
import pymongo
#方法一
client = pymongo.MongoClient(host='localhost', port=27017)
#方法二
client = MongoClient('mongodb://localhost:27017/')

#二.【连接mongo指定数据库’test ‘】:
#方法一
db = client.test
#方法二
db = client['test']

#三.【连接指定集合(Collection)如:students】
#方法一
collection = db.students
#方法二
collection = db['students']

二、【插入数据新版本】在用:

'''
实际上在 PyMongo 3.X 版本中,insert() 方法官方已经不推荐使用了,当然继续使用也没有什么问题,官方推荐使用 insert_one() 和 insert_many() 方法将插入单条和多条记录分开
'''
# A.【insert_one()
student = {
'id': '',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}
result = collection.insert_one(student)
print(result)
print(result.inserted_id)
'''
运行结果:
<pymongo.results.InsertOneResult object at 0x10d68b558>
5932ab0f15c2606f0c1cf6c5
返回结果和 insert() 方法不同,这次返回的是InsertOneResult 对象,我们可以调用其 inserted_id 属性获取 _id
''' #B.【insert_many() 方法,我们可以将数据以列表形式传递即可】
student1 = {
'id': '',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}
student2 = {
'id': '',
'name': 'Mike',
'age': 21,
'gender': 'male'
}
result = collection.insert_many([student1, student2])
print(result)
print(result.inserted_ids)
'''
insert_many() 方法返回的类型是 InsertManyResult,调用inserted_ids 属性可以获取插入数据的 _id 列表,运行结果:
<pymongo.results.InsertManyResult object at 0x101dea558>
[ObjectId('5932abf415c2607083d3b2ac'), ObjectId('5932abf415c2607083d3b2ad')]
'''

二、插入数据老版本(不建议用):

#四.【插入数据,在students内,用字典】
student = {
'id': '',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}
result = collection.insert(student)
print(result)
'''
在 MongoDB 中,每条数据其实都有一个 _id 属性来唯一标识,如果没有显式指明 _id,MongoDB 会自动产生一个 ObjectId 类型的 _id 属性。insert() 方法会在执行后返回的 _id 值:
5932a68615c2606814c91f3d
'''

#【插入多个字典数据,用列表把字典组合】:
student1 = {
'id': '',
'name': 'Jordan',
'age': 20,
'gender': 'male'
}
student2 = {
'id': '',
'name': 'Mike',
'age': 21,
'gender': 'male'
}
result = collection.insert([student1, student2])
print(result)
'''
返回的结果是对应的 _id 的集合,运行结果:
[ObjectId('5932a80115c2606a59e8a048'), ObjectId('5932a80115c2606a59e8a049')]
'''

三、【查询一】用 find_one() 或 find() 方法进行查询,find_one() 查询得到是单个结果,find() 则返回一个生成器对象。

result = collection.find_one({'name': 'Mike'})
print(type(result))
print(result)
'''
在这里我们查询 name 为 Mike 的数据,它的返回结果是字典类型,运行结果:<class 'dict'>
{'_id': ObjectId('5932a80115c2606a59e8a049'), 'id': '20170202', 'name': 'Mike', 'age': 21, 'gender': 'male'}
可以发现它多了一个 _id 属性,这就是 MongoDB 在插入的过程中自动添加的。
'''

三、【查询二】单条数据查询

from bson.objectid import ObjectId
result = collection.find_one({'_id': ObjectId('593278c115c2602667ec6bae')})
print(result)
'''
其查询结果依然是字典类型,运行结果:
{'_id': ObjectId('593278c115c2602667ec6bae'), 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}'''

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

#
results = collection.find({'age': 20})
print(results)
for result in results:
print(result)
'''
运行结果:
<pymongo.cursor.Cursor object at 0x1032d5128>
{'_id': ObjectId('593278c115c2602667ec6bae'), 'id': '20170101', 'name': 'Jordan', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('593278c815c2602678bb2b8d'), 'id': '20170102', 'name': 'Kevin', 'age': 20, 'gender': 'male'}
{'_id': ObjectId('593278d815c260269d7645a8'), 'id': '20170103', 'name': 'Harden', 'age': 20, 'gender': 'male'}
'''

如果要查询年龄大于 20 的数据,则写法如下:

results = collection.find({'age': {'$gt': 20}})

mongoDB的使用A的更多相关文章

  1. 【翻译】MongoDB指南/聚合——聚合管道

    [原文地址]https://docs.mongodb.com/manual/ 聚合 聚合操作处理数据记录并返回计算后的结果.聚合操作将多个文档分组,并能对已分组的数据执行一系列操作而返回单一结果.Mo ...

  2. 【翻译】MongoDB指南/CRUD操作(四)

    [原文地址]https://docs.mongodb.com/manual/ CRUD操作(四) 1 查询方案(Query Plans) MongoDB 查询优化程序处理查询并且针对给定可利用的索引选 ...

  3. 【翻译】MongoDB指南/CRUD操作(三)

    [原文地址]https://docs.mongodb.com/manual/ CRUD操作(三) 主要内容: 原子性和事务(Atomicity and Transactions),读隔离.一致性和新近 ...

  4. 【翻译】MongoDB指南/CRUD操作(二)

    [原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(二) 主要内容: 更新文档,删除文档,批量写操作,SQL与MongoDB映射图,读隔离(读关 ...

  5. 【翻译】MongoDB指南/CRUD操作(一)

    [原文地址]https://docs.mongodb.com/manual/ MongoDB CRUD操作(一) 主要内容:CRUD操作简介,插入文档,查询文档. CRUD操作包括创建.读取.更新和删 ...

  6. CRL快速开发框架系列教程十二(MongoDB支持)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  7. MongoDB系列(二):C#应用

    前言 上一篇文章<MongoDB系列(一):简介及安装>已经介绍了MongoDB以及其在window环境下的安装,这篇文章主要讲讲如何用C#来与MongoDB进行通讯.再次强调一下,我使用 ...

  8. MongoDB系列(一):简介及安装

    什么是MongoDB MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为应用提供可扩展的高 ...

  9. [原]分享一下我和MongoDB与Redis那些事

    缘起:来自于我在近期一个项目上遇到的问题,在Segmentfault上发表了提问 知识背景: 对不是很熟悉MongoDB和Redis的同学做一下介绍. 1.MongoDB数组查询:MongoDB自带L ...

  10. 用MongoDB分析合肥餐饮业

    看了<从数据角度解析福州美食>后难免心痒,动了要分析合肥餐饮业的念头,因此特地写了Node.js爬虫爬取了合肥的大众点评数据.分析数据库我并没有采用MySQL而是用的MongoDB,是因为 ...

随机推荐

  1. SQL常用语法大全

    一. Table 增加列 1.增加列:alter table tableName add columnName varchar(30) 1.2. 修改列类型:alter table tableName ...

  2. javascript替代Array.prototype.some操作

    Array.prototype.some在低版本浏览器好像不太兼容,下列是替代方法 一. for 循环 const initIds: any[] = [1,2,3]; const Ids: any[] ...

  3. 下拉菜单被表单、图片、FLASH挡住的解决办法

    设置Flash的参数: <param name="wmode" value="opaque"> <object classid="c ...

  4. IDEA与Elicpse

    IDEA的项目 = Elicpse的工作区 Elicpse的项目 = IDEA的模块 修改信息提示 Alt+/ 关闭当前窗口 Ctrl+W

  5. PHP策略模式2

    <?php /** PHP 策略模式 * 策略模式是对象的行为模式,用意是对一组算法的封装.动态的选择需要的算法并使用. * 策略模式指的是程序中涉及决策控制的一种模式.策略模式功能非常强大,因 ...

  6. IOP知识点(2)

    1   URL资源访问不足时,需要添加URL权限 2   重定向问题解决办法:3  cloud-service-factory 项目 gradlew方法 1   URL资源访问不足时,需要添加URL权 ...

  7. MySQL高效的前提

    好硬件是数据库高效的前提,没有好硬件其他优化都是白费 高性能的CPU 主频高SQL处理的更快 3级cache大CPU计算速率更快 多线程,同时并发处理SQL 关闭NUMA并设置为最大性能模式,充分利用 ...

  8. 【LeetCode每天一题】Median of Two Sorted Arrays(两数组中的中位数)

    There are two sorted arrays nums1 and nums2 of size m and n respectively.  Find the median of the tw ...

  9. [文摘]那些一心想要离开 BAT 的人,后来怎么样了?

    人心是个无底洞,填不满也掏不空 <Working at Google seemed like a dream job. The reality has been a tedious, point ...

  10. Amber TUTORIAL B1: Simulating a DNA polyA-polyT Decamer

    Section 1: Introduction The input files required (using their default file names): prmtop - a file c ...