笔记9:MongoDB数据库操作

import pymongo

client=pymongo.MongoClient('localhost',27017) #localhost 指的是本地,激活本地mongo客户端,创建本地MongoDB

walden=client['walden'] #左边是python对象,中括号中的是数据库中的名称,建议对象和名称一致  类似创建 excel表

sheet_lines=walden['sheet_lines']  #类似于excel表中的第一个表单

# path = r'F:\5-学习\python学习\20170111\walden.txt'

#

# with open(path, 'r') as f:

#    lines = f.readlines()

#    for index,words in enumerate(lines):

#        data = {

#            'index':index,

#            'line':words,

#            'word':len(words.split())

#        }

#        sheet_lines.insert_one(data)

# $lt/$lte/$gt/$gte/$ne,依次等价于/>=/!=。(l表示less g表示greater e表示equal n表示not  )

foriteminsheet_lines.find({'word':{'$lt':5}}):#查找小于5个单词的表项

print(item)


知识小结:

1、数据库的建立和表单的创建

2、数据库中数据插入、查询,按照条件查询,后续还需要学习数据库更多的操作方法

你可能感兴趣的:(笔记9:MongoDB数据库操作)