mongoDB将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于 JSON 对象。
mongoDB中的 database—>collections—>document—>field —>index.
相当于关系系数据库中的 database—>table —>rows —>column —>index.
一个document信息
{
"_id":ObjectId(1234567890),
"age":16,
"name":"zixuezhe",
"email":"[email protected]"
}
from pymongo import MongoClient
class CollectionMongo():
def __init__(self,host,port):
self.host = host
self.port = port
self.db = None
self.my_set = None
self.conn = None
try:
self.conn = MongoClient(self.host,self.port)
except Exception,e:
print e
def mySet(self,database,collection):
self.database = database
self.collection = collection
try:
self.db = self.conn[database]
self.my_set = self.db[collection]
return self.my_set
except Exception,e:
print e
if __name__ == "__main__":
collectionMongo = CollectionMongo('XXX.XXX.XXX.XXX', 27017)
my_set = collectionMongo.mySet('sun','col')
my_set.insert({'name':'zhangsan','age':18},{'name':'lisi','age':19})
for i in my_set.find():
print i
使用db.database.collecction.find()可以查到集合内所有数据
if __name__ == "__main__":
collectionMongo = CollectionMongo('XXX.XXX.XXX.XXX', 27017)
my_set = collectionMongo.mySet('sun','col')
for i in my_set.find():
print i
使用find({“name”:“zhangsan”})可以查到所有name为zhangsan的bson数据。相当于sql select * from
database where name='zhangsan'
for i in my_set.find({"name":"zhangsan"}):
print i
使用find_one({“name”:“zhangsan”})会返回第一条匹配的数据
my_set.update({"name":"zhangsan"},{'$set':{"name":"lisi"}})
for i in my_set.find():
print i
将"name":“zhangsan"修改为"lisi”,这种用法只会将第一条"name"为"zhangsan"的数据修改为"lisi"。
my_set.update({"name":"zhangsan"},{'$set':{"name":"lisi"}},upsert=True,multi=True)
for i in my_set.find():
print i
使用upsert的作用的如果数据库没有匹配到数据,会将这条数据插入(默认为false),multi是批量操作,默认是false,他的作用的更新所有匹配信息
删除name=lisi的全部记录
my_set.remove({'name':'lisi'})
根据name=lisi的id,删除对应的数据
id = my_set.find_one({'name':'lisi'})['_id']
my_set.remove(id)
查询成绩及格的同学:
for i in my_set.find('grade':{"$gte":60})
print i
根据类型判断,找出name为String类型的数据:
for i in my_set.find('name':{'$type':2})
其中2表示的是String类型:部分对照
Double 1
String 2
Object 3
Array 4
Binary data 5
Undefined 6 已废弃
Object id 7
Boolean 8
Date 9
Null 10
排序sort().其中1代表升序,-1代表降序
for i in my_set.find().sort([("age",1)]):
print(i)
limit和skip
#limit()方法用来读取指定数量的数据
#skip()方法用来跳过指定数量的数据
#下面表示跳过两条数据后读取6条
for i in my_set.find().skip(2).limit(6):
print(i)
IN
#找出age是20、30、35的数据
for i in my_set.find({"age":{"$in":(20,30,35)}}):
print(i)
OR
#找出age是20或35的记录
for i in my_set.find({"$or":[{"age":20},{"age":35}]}):
print(i)
all
'''
dic = {"name":"lisi","age":18,"li":[1,2,3]}
dic2 = {"name":"zhangsan","age":18,"li":[1,2,3,4,5,6]}
my_set.insert(dic)
my_set.insert(dic2)
'''
for i in my_set.find({'li':{'$all':[1,2,3,4]}}):
print(i)
all和in的区别,all表示必须存在指定全部条件,in是存在某一个指定条件
push/pushAll
my_set.update({'name':"lisi"}, {'$push':{'li':4}})
for i in my_set.find({'name':"lisi"}):
print(i)
#输出:{'li': [1, 2, 3, 4], '_id': ObjectId('58c50d784fc9d44ad8f2e803'), 'age': 18, 'name': 'lisi'}
my_set.update({'name':"lisi"}, {'$pushAll':{'li':[4,5]}})
for i in my_set.find({'name':"lisi"}):
print(i)
#输出:{'li': [1, 2, 3, 4, 4, 5], 'name': 'lisi', 'age': 18, '_id': ObjectId('58c50d784fc9d44ad8f2e803')}
pop/pull/pullAll
#pop
#移除最后一个元素(-1为移除第一个)
my_set.update({'name':"lisi"}, {'$pop':{'li':1}})
for i in my_set.find({'name':"lisi"}):
print(i)
#输出:{'_id': ObjectId('58c50d784fc9d44ad8f2e803'), 'age': 18, 'name': 'lisi', 'li': [1, 2, 3, 4, 5]}
#pull (按值移除)
#移除3
my_set.update({'name':"lisi"}, {'$pop':{'li':3}})
#pullAll (移除全部符合条件的)
my_set.update({'name':"lisi"}, {'$pullAll':{'li':[1,2,3]}})
for i in my_set.find({'name':"lisi"}):
print(i)
#输出:{'name': 'lisi', '_id': ObjectId('58c50d784fc9d44ad8f2e803'), 'li': [4, 5], 'age': 18}
MongoDB的更多操作,还需要慢慢学习。