# Created by xiaosanyu at 16/5/30
# https://docs.mongodb.com/ecosystem/drivers/python/
# https://pypi.python.org/pypi/pymongo/
import pymongo
from pymongo.mongo_client import MongoClient
import pymongo.errors
import traceback
try:
# 连接到 mongodb 服务
mongoClient = MongoClient('localhost', 27017)
# 连接到数据库
mongoDatabase = mongoClient.test
print("connect database successfully")
# 获取集合
mongoCollection = mongoDatabase.COMPANY
# 移除所有数据
mongoCollection.remove()
# 添加数据
mongoCollection.insert_many([{"Name": "Paul", "Age": "32", "Address": "California", "Salary": "20000.00"},
{"Name": "Allen", "Age": "25", "Address": "Texas", "Salary": "15000.00"},
{"Name": "Teddy", "Age": "23", "Address": "Norway", "Salary": "20000.00"},
{"Name": "Mark", "Age": "25", "Address": "Rich-Mond", "Salary": "65000.00"},
{"Name": "David", "Age": "27", "Address": "Texas", "Salary": "85000.00"},
{"Name": "Kim", "Age": "22", "Address": "South-Hall", "Salary": "45000.00"},
{"Name": "James", "Age": "24", "Address": "Houston", "Salary": "10000.00"}, ])
#获取集合中的值
for row in mongoCollection.find():
print("-" * 50) # 输出50个-,作为分界线
print("%-10s %s" % ("_id", row['_id'])) # 字段名固定10位宽度,并且左对齐
print("%-10s %s" % ("name", row['Name']))
print("%-10s %s" % ("age", row['Age']))
print("%-10s %s" % ("address", row['Address']))
print("%-10s %s" % ("salary", row['Salary']))
print('\n\n\n')
# 使id自增
mongoCollection.remove()
# 创建计数表
mongoDatabase.counters.save({"_id": "people_id", "sequence_value": 0})
# 创建存储过程
mongoDatabase.system_js.getSequenceValue = '''function getSequenceValue(sequenceName){
var sequenceDocument = db.counters.findAndModify({
query: {_id: sequenceName},
update: {$inc:{sequence_value: 1}},
new:true
});
return sequenceDocument.sequence_value;
}'''
mongoCollection.insert_many(
[{"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "Paul", "Age": "32",
"Address": "California", "Salary": "20000.00"},
{"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "Allen", "Age": "25",
"Address": "Texas", "Salary": "15000.00"},
{"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "Teddy", "Age": "23",
"Address": "Norway", "Salary": "20000.00"},
{"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "Mark", "Age": "25",
"Address": "Rich-Mond", "Salary": "65000.00"},
{"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "David", "Age": "27",
"Address": "Texas", "Salary": "85000.00"},
{"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "Kim", "Age": "22",
"Address": "South-Hall", "Salary": "45000.00"},
{"_id": mongoDatabase.eval("getSequenceValue('people_id')"), "Name": "James", "Age": "24",
"Address": "Houston", "Salary": "10000.00"}, ])
for row in mongoCollection.find():
print("-" * 50) # 输出50个-,作为分界线
print("%-10s %s" % ("_id", int(row['_id']))) # 字段名固定10位宽度,并且左对齐
print("%-10s %s" % ("name", row['Name']))
print("%-10s %s" % ("age", row['Age']))
print("%-10s %s" % ("address", row['Address']))
print("%-10s %s" % ("salary", row['Salary']))
except pymongo.errors.PyMongoError as e:
print("mongo Error:", e)
traceback.print_exc()

sanxiaochengyu
- 粉丝: 273
最新资源
- 如何借助AI+数智应用工具提升科技平台的服务效率?.docx
- 如何借助AI+数智应用快速生成高质量的科技活动策划方案?.docx
- 如何借助AI+数智应用破解科技管理系统“缺乏价值创造”的难题?.docx
- 如何借助AI+数智应用手段激活科技管理系统的内外部资源?.docx
- 如何借助AI+数智应用提升大学科技管理的效能与价值?.docx
- 如何借助AI+数智应用有效构建科技资源平台以解决资源丰富性问题?.docx
- 如何利用AI+数智应用技术提升科技管理工作的价值创造能力?.docx
- 如何利用AI+数智应用技术提升科技管理工作的效率和创新能力?.docx
- 如何利用AI+数智应用让单场科技活动产生长期效益?.docx
- 如何利用AI+数智应用全流程创新诊断快速提升科技管理系统的智能化水平?.docx
- 如何利用AI+数智应用让科技管理工作既高效又具备创新性?.docx
- 如何利用AI+数智应用新产品提升政府科技监测的价值创造能力?.docx
- 如何让技术供需对接活动的匹配通过AI+数智应用更精准,避免资源浪费?.docx
- 如何通过AI+数智应用服务提升科技活动组织效率和资源匹配精准度?.docx
- 如何通过AI+数智应用工具解决科技管理中的环节性痛点?.docx
- 如何通过AI+数智应用工具提升科技活动供需匹配效率?.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


