1. 程式人生 > >python 連接mongodb ,並將EXCEL文檔導入mongodb

python 連接mongodb ,並將EXCEL文檔導入mongodb

dump filter 效率 world 升序 import ces 基於 http

一、下載軟件

1.https://pypi.python.org/pypi/pymongo/#downloads 下載 PYMONGO

下載後 用命令提示符 cmd進入目錄 並運行命令 python setup.py install

2.下載 xlrd https://pypi.python.org/pypi/xlrd

利用pymongo包進行數據庫的連接,使用xlrd包讀取excel數據,由於二者數據結構的不同,要將excel格式數據轉換為json格式數據。由於編碼問題會出現“TypeError: ‘str‘ object does not support item assignment”,要利用json.loads方法對數據進行解碼 代碼如下 #coding=utf-8
import xlrd import sys import json import pymongo from pymongo import MongoClient #連接數據庫 client=MongoClient(‘localhost‘,27017) db=client.scrapy account=db.weibo data=xlrd.open_workbook(‘test.xlsx‘) table=data.sheets()[0] #讀取excel第一行數據作為存入mongodb的字段名 rowstag=table.row_values(0) nrows=table.nrows
#ncols=table.ncols #print rows returnData={} for i in range(1,nrows): #將字段名和excel數據存儲為字典形式,並轉換為json格式 returnData[i]=json.dumps(dict(zip(rowstag,table.row_values(i)))) #通過編解碼還原數據 returnData[i]=json.loads(returnData[i]) #print returnData[i] account.insert(returnData[i])

MongoDB

是由C++語言編寫的,是一個基於分布式文件存儲的開源數據庫系統。

在高負載的情況下,添加更多的節點,可以保證服務器性能。

MongoDB 旨在為WEB應用提供可擴展的高性能數據存儲解決方案。

MongoDB 將數據存儲為一個文檔,數據結構由鍵值(key=>value)對組成。MongoDB 文檔類似於 JSON 對象。字段值可以包含其他文檔,數組及文檔數組。

1.創建連接

技術分享
import pymongo

client = pymongo.MongoClient(‘mongodb://localhost:27017‘)
#或
#client = pymongo.MongoClient(‘localhost‘,‘27017‘)

## 如果設置了權限,註意xxx用戶權限要可以cover到後面使用到的數據庫
# client = pymongo.MongoClient(‘10.134.80.119‘, 20000, username=‘xxx‘, password=‘xxx‘)
技術分享

2.連接數據庫

#操作test數據庫
db_name = ‘test‘ db = client[db_name]

3.選擇要操作的集合(表)

collection_set01 = db[‘set01‘]

下面就可以使用collection_set01進行增刪改查操作了.

4.增刪改查操作

‘‘‘

###################--插入文檔--####################
save() vs insert()
mongodb的save和insert函數都可以向collection裏插入數據,但兩者是有兩個區別
1. save函數實際就是根據參數條件,調用了insert或update函數.如果想插入的數據對象存在,insert函數會報錯,而save函數是改變原來的對象;如果想插入的對象不存在,那麽它們執行相同的插入操作.這裏可以用幾個字來概括它們兩的區別,即所謂"有則改之,無則加之".
2. insert可以一次性插入一個列表,而不用遍歷,效率高, save則需要遍歷列表,一個個插入.
‘‘‘

技術分享
record_l = [
{‘_id‘:0,‘name‘: ‘zzzzz‘,‘age‘: -27,‘high‘: 176},
{‘_id‘:1,‘name‘: ‘zhangweijian‘,‘age‘: 27,‘high‘: 171},
{‘_id‘:2,‘name‘: ‘zhang‘,‘age‘: 26,‘high‘: 173},
{‘_id‘:3,‘name‘: ‘wei‘,‘age‘: 29,‘high‘: 180},
{‘_id‘:4,‘name‘: ‘weijian‘,‘age‘: 30,‘high‘: 158},
{‘_id‘:5,‘name‘: ‘zhangjian‘,‘age‘: 22,‘high‘: 179},
{‘_id‘:6,‘name‘: ‘zwj‘,‘age‘: 19,‘high‘: 166},
{‘_id‘:100,‘name‘: ‘zwj‘,‘age‘: 19,‘list‘:[2,3,5]},
{‘_id‘:101,‘name‘: ‘zwj‘,‘age‘: 19,‘list‘:[1,2,3,4,5,6,7]},
]
try:
    for record in record_l:
        collection_set01.save(record)
except pymongo.errors.DuplicateKeyError:
    print ‘record exists‘
except Exception as e:
    print e
技術分享

####################--刪除數據--####################
remove()
delete_one(self, filter, collation=None)
delete_many(self, filter, collation=None)
>>> db.test.count({‘x‘: 1})
3
>>> result = db.test.delete_one({‘x‘: 1})
>>> result.deleted_count
1
>>> db.test.count({‘x‘: 1})
2

:Parameters:
- `filter`: A query that matches the document to delete.
- `collation` (optional): An instance of
:class:`~pymongo.collation.Collation`. This option is only supported
on MongoDB 3.4 and above.

:Returns:
- An instance of :class:`~pymongo.results.DeleteResult`.
‘‘‘

技術分享
newinsert1 = {‘_id‘:7,‘comment‘:‘test delete‘}
newinsert2 = {‘_id‘:8,‘comment‘:‘test delete‘}
newinsert3 = {‘_id‘:9,‘comment‘:‘test delete‘}
collection_set01.save(newinsert1)
collection_set01.save(newinsert2)
collection_set01.save(newinsert3)

remove_before = collection_set01.find()
print ‘delete before‘
for obj in remove_before:
    print obj

collection_set01.delete_many({‘_id‘:{‘$gt‘:6,‘$lt‘:100}})   #刪除所有滿足條件的文檔,刪除_id大於6,小於100
collection_set01.delete_one({‘_id‘:6})   #刪除一條滿足條件的文檔,刪除_id=6
#collection_set01.delete_many({}) #刪除整個集合
remove_after = collection_set01.find()
print ‘delete after‘
for obj in remove_after:
    print obj

#輸出結果
delete before
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
{u‘high‘: 166, u‘age‘: 19, u‘_id‘: 6, u‘name‘: u‘zwj‘}
{u‘comment‘: u‘test delete‘, u‘_id‘: 7}
{u‘comment‘: u‘test delete‘, u‘_id‘: 8}
{u‘comment‘: u‘test delete‘, u‘_id‘: 9}
delete after
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
技術分享

‘‘‘
###################--更新數據--####################
replace_one(self, filter, replacement, upsert=False, bypass_document_validation=False, collation=None)
update_one(self, filter, update, upsert=False, bypass_document_validation=False, collation=None)
update_many(self, filter, update, upsert=False, bypass_document_validation=False, collation=None)
‘‘‘

技術分享
collection_set01.replace_one({‘_id‘: 1},{‘comment‘: ‘after replace_one operation just exists comment(key)‘})  #replace_one用指定的key-value替代原來所有的key-value
collection_set01.update_one({ "high" : { ‘$gt‘ : 170 } } , { ‘$set‘ : { "comment" : "更新於身高高於170的一條記錄"}}) #update_one更新已經對應的key-value,其它不變
collection_set01.update_many({‘high‘:{‘$gt‘:171}},{‘$set‘:{‘comment‘:‘use update_many‘}})  #同上,能夠update所有符合匹配條件的文檔
技術分享

‘‘‘
########################--查詢--###################
find(self, filter=None, *args, **kwargs)
find_one(self, filter=None, *args, **kwargs)
params:projection/limit/skip
‘‘‘

 find方法源碼

技術分享
find(self, *args, **kwargs) method of pymongo.collection.Collection instance
    Query the database.

    The `filter` argument is a prototype document that all results
    must match. For example:

    >>> db.test.find({"hello": "world"})

    only matches documents that have a key "hello" with value
    "world".  Matches can have other keys *in addition* to
    "hello". The `projection` argument is used to specify a subset
    of fields that should be included in the result documents. By
    limiting results to a certain subset of fields you can cut
    down on network traffic and decoding time.
Raises :class:`TypeError` if any of the arguments are of improper type. Returns an instance of :class:`~pymongo.cursor.Cursor` corresponding to this query. The :meth:`find` method obeys the :attr:`read_preference` of this :class:`Collection`. :Parameters: - `filter` (optional): a SON object specifying elements which must be present for a document to be included in the result set - `projection` (optional): a list of field names that should be returned in the result set or a dict specifying the fields to include or exclude. If `projection` is a list "_id" will always be returned. Use a dict to exclude fields from the result (e.g. projection={‘_id‘: False}). - `skip` (optional): the number of documents to omit (from the start of the result set) when returning the results - `limit` (optional): the maximum number of results to return - `no_cursor_timeout` (optional): if False (the default), any returned cursor is closed by the server after 10 minutes of inactivity. If set to True, the returned cursor will never time out on the server. Care should be taken to ensure that cursors with no_cursor_timeout turned on are properly closed. - `cursor_type` (optional): the type of cursor to return. The valid options are defined by :class:`~pymongo.cursor.CursorType`: - :attr:`~pymongo.cursor.CursorType.NON_TAILABLE` - the result of this find call will return a standard cursor over the result set. - :attr:`~pymongo.cursor.CursorType.TAILABLE` - the result of this find call will be a tailable cursor - tailable cursors are only for use with capped collections. They are not closed when the last data is retrieved but are kept open and the cursor location marks the final document position. If more data is received iteration of the cursor will continue from the last document received. For details, see the `tailable cursor documentation <http://www.mongodb.org/display/DOCS/Tailable+Cursors>`_. - :attr:`~pymongo.cursor.CursorType.TAILABLE_AWAIT` - the result of this find call will be a tailable cursor with the await flag set. The server will wait for a few seconds after returning the full result set so that it can capture and return additional data added during the query. - :attr:`~pymongo.cursor.CursorType.EXHAUST` - the result of this find call will be an exhaust cursor. MongoDB will stream batched results to the client without waiting for the client to request each batch, reducing latency. See notes on compatibility below. - `sort` (optional): a list of (key, direction) pairs specifying the sort order for this query. See :meth:`~pymongo.cursor.Cursor.sort` for details. - `allow_partial_results` (optional): if True, mongos will return partial results if some shards are down instead of returning an error. - `oplog_replay` (optional): If True, set the oplogReplay query flag. - `modifiers` (optional): A dict specifying the MongoDB `query modifiers`_ that should be used for this query. For example:: >>> db.test.find(modifiers={"$maxTimeMS": 500}) - `batch_size` (optional): Limits the number of documents returned in a single batch. - `manipulate` (optional): **DEPRECATED** - If True (the default), apply any outgoing SON manipulators before returning. - `collation` (optional): An instance of :class:`~pymongo.collation.Collation`. This option is only supported on MongoDB 3.4 and above. .. note:: There are a number of caveats to using :attr:`~pymongo.cursor.CursorType.EXHAUST` as cursor_type: - The `limit` option can not be used with an exhaust cursor. - Exhaust cursors are not supported by mongos and can not be used with a sharded cluster. The :meth:`find` method obeys the :attr:`read_preference` of this :class:`Collection`. :Parameters: - `filter` (optional): a SON object specifying elements which must be present for a document to be included in the result set - `projection` (optional): a list of field names that should be returned in the result set or a dict specifying the fields to include or exclude. If `projection` is a list "_id" will always be returned. Use a dict to exclude fields from the result (e.g. projection={‘_id‘: False}). - `skip` (optional): the number of documents to omit (from the start of the result set) when returning the results - `limit` (optional): the maximum number of results to return - `no_cursor_timeout` (optional): if False (the default), any returned cursor is closed by the server after 10 minutes of inactivity. If set to True, the returned cursor will never time out on the server. Care should be taken to ensure that cursors with no_cursor_timeout turned on are properly closed. - `cursor_type` (optional): the type of cursor to return. The valid options are defined by :class:`~pymongo.cursor.CursorType`: - :attr:`~pymongo.cursor.CursorType.NON_TAILABLE` - the result of this find call will return a standard cursor over the result set. - :attr:`~pymongo.cursor.CursorType.TAILABLE` - the result of this find call will be a tailable cursor - tailable cursors are only for use with capped collections. They are not closed when the last data is retrieved but are kept open and the cursor location marks the final document position. If more data is received iteration of the cursor will continue from the last document received. For details, see the `tailable cursor documentation <http://www.mongodb.org/display/DOCS/Tailable+Cursors>`_. - :attr:`~pymongo.cursor.CursorType.TAILABLE_AWAIT` - the result of this find call will be a tailable cursor with the await flag set. The server will wait for a few seconds after returning the full result set so that it can capture and return additional data added during the query. - :attr:`~pymongo.cursor.CursorType.EXHAUST` - the result of this find call will be an exhaust cursor. MongoDB will stream batched results to the client without waiting for the client to request each batch, reducing latency. See notes on compatibility below. - `sort` (optional): a list of (key, direction) pairs specifying the sort order for this query. See :meth:`~pymongo.cursor.Cursor.sort` for details. - `allow_partial_results` (optional): if True, mongos will return partial results if some shards are down instead of returning an error. - `oplog_replay` (optional): If True, set the oplogReplay query flag. - `modifiers` (optional): A dict specifying the MongoDB `query modifiers`_ that should be used for this query. For example:: >>> db.test.find(modifiers={"$maxTimeMS": 500}) - `batch_size` (optional): Limits the number of documents returned in a single batch. - `manipulate` (optional): **DEPRECATED** - If True (the default), apply any outgoing SON manipulators before returning. - `collation` (optional): An instance of :class:`~pymongo.collation.Collation`. This option is only supported on MongoDB 3.4 and above.
技術分享

#直接上代碼

技術分享
#1.查詢身高小於180的文檔
print ‘-------------身高小於180:‘
print type(collection_set01.find({‘high‘:{‘$lt‘:180}})) #<class ‘pymongo.cursor.Cursor‘>
for r in collection_set01.find({‘high‘:{‘$lt‘:180}}):
    print r
print type(collection_set01.find_one({‘high‘:{‘$lt‘:180}})) #<type ‘dict‘>
print ‘use find_one:‘,collection_set01.find_one({‘high‘:{‘$lt‘:180}})[‘high‘]
print ‘use find_one:‘,collection_set01.find_one({‘high‘:{‘$lt‘:180}})

#2.查詢特定鍵(select key1,key2 from table;)
print ‘-------------查詢特定鍵--------‘
print ‘-------------查詢身高大於170,並只列出_id,high和age字段(使用列表形式_id默認打印出來,可以使用{}忽視_id):‘
for r in collection_set01.find({‘high‘:{‘$gt‘:170}},projection=[‘high‘,‘age‘]):print r
print ‘\n‘
print ‘--------------skip參數用法‘
for r in collection_set01.find({‘high‘:{‘$gt‘:170}},[‘high‘,‘age‘],skip=1):print r #skip=1跳過第一個匹配到的文檔
for r in collection_set01.find({‘high‘:{‘$gt‘:170}},[‘high‘,‘age‘]).skip(1):print r #skip=1跳過第一個匹配到的文檔
print ‘\n‘
print ‘--------------limit參數用法‘
for r in collection_set01.find({‘high‘:{‘$gt‘:170}},[‘high‘,‘age‘],limit=1):print r #limit=2限制顯示2條文檔
print ‘\n‘
print ‘--------------用{}描述特定鍵‘
for r in collection_set01.find({‘high‘:{‘$gt‘:170}},{‘high‘:1,‘age‘:1,‘_id‘:False}):print r

print ‘---------------------多條件查詢‘
print collection_set01.find_one({‘high‘:{‘$gt‘:10},‘age‘:{‘$lt‘:26,‘$gt‘:10}})

#3.$in
print ‘----------------IN‘
for r in collection_set01.find({"age":{"$in":[23, 26, 32]}}): print r  # select * from users where age in (23, 26, 32)
#for u in db.users.find({"age":{"$nin":(23, 26, 32)}}): print u # select * from users where age not in (23, 26, 32)

#4.count統計數目
print ‘----------------count‘
print collection_set01.find({"age":{"$gt":20}}).count() # select count(*) from set01 where age > 10

#5.$or
print ‘----------------條件或‘
print ‘大於等於29或者小於23‘
for r in collection_set01.find({"$or":[{"age":{"$lte":23}}, {"age":{"$gte":29}}]}): print r

#6.$exists,是否存在字段
print ‘------------exists‘
for r in collection_set01.find({‘age‘:{‘$exists‘:True}}):print ‘age exists‘,r # select * from 集合名 where exists 鍵1
for r in collection_set01.find({‘age‘:{‘$exists‘:False}}):print ‘age not exists‘,r # select * from 集合名 where not exists 鍵1

#7.正則表達式查詢
print ‘正則表達式查詢‘
#method 1
for r in collection_set01.find({‘name‘:{‘$regex‘:r‘.*wei.*‘}}):print r   #找出name字段中包含wei的文檔
#method 2
import re
Regex = re.compile(r‘.*zhang.*‘,re.IGNORECASE)
for r in collection_set01.find({‘name‘:Regex}):print r   #找出name字段中包含wei的文檔


#8.sort排序

print ‘--------------------使用sort排序(文檔中沒有排序的字段也會打印出來,表示最小)‘
#pymongo.ASCENDING      1
#pymongo.DESCENDING     -1
#sort([[],()]),[],()均可
print ‘--------------age 升序‘
for r in collection_set01.find().sort([["age",pymongo.ASCENDING]]):print r
print ‘--------------age 降序‘
for r in collection_set01.find().sort([("age",-1)]):print r
print ‘--------------age升序,high升序‘
for r in collection_set01.find().sort((("age",pymongo.ASCENDING),("high",pymongo.ASCENDING))):print r
print ‘--------------age升序,high降序‘
for r in collection_set01.find(sort=[("age",pymongo.ASCENDING),("high",pymongo.ASCENDING)]):print r


#9.$all判斷數組屬性是否包含全部條件,註意與$in區別

print ‘-------------$all‘
for r in collection_set01.find({‘list‘:{‘$all‘:[2,3,4]}}):print r
print ‘-------------$in‘
for r in collection_set01.find({‘list‘:{‘$in‘:[2,3,4]}}):print r


#10.$size匹配數組屬性元素數量
print ‘-------------$size‘
print ‘-------------size=3‘
for r in collection_set01.find({‘list‘:{‘$size‘:3}}):print r
print ‘-------------size=7‘
for r in collection_set01.find({‘list‘:{‘$size‘:7}}):print r

#11.$unset和$set相反表示移除文檔屬性
print ‘-------------------$unset‘
print ‘---before‘
for r in collection_set01.find({‘name‘:‘weijian‘}):print r
collection_set01.update({‘name‘:‘weijian‘},{‘$unset‘:{‘age‘:1}})
print ‘---after‘
for r in collection_set01.find({‘name‘:‘weijian‘}):print r


#輸出結果
-------------查詢測試-----------
-------------身高小於180:
<class ‘pymongo.cursor.Cursor‘>
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
<type ‘dict‘>
use find_one: 173
use find_one: {u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
-------------查詢特定鍵--------
-------------查詢身高大於170,並只列出_id,high和age字段(使用列表形式_id默認打印出來,可以使用{}忽視_id):
{u‘high‘: 173, u‘age‘: 26, u‘_id‘: 2}
{u‘high‘: 180, u‘age‘: 29, u‘_id‘: 3}
{u‘high‘: 179, u‘age‘: 22, u‘_id‘: 5}
{u‘high‘: 176, u‘age‘: -27, u‘_id‘: 0}


--------------skip參數用法
{u‘high‘: 180, u‘age‘: 29, u‘_id‘: 3}
{u‘high‘: 179, u‘age‘: 22, u‘_id‘: 5}
{u‘high‘: 176, u‘age‘: -27, u‘_id‘: 0}
{u‘high‘: 180, u‘age‘: 29, u‘_id‘: 3}
{u‘high‘: 179, u‘age‘: 22, u‘_id‘: 5}
{u‘high‘: 176, u‘age‘: -27, u‘_id‘: 0}


--------------limit參數用法
{u‘high‘: 173, u‘age‘: 26, u‘_id‘: 2}


--------------用{}描述特定鍵
{u‘high‘: 173, u‘age‘: 26}
{u‘high‘: 180, u‘age‘: 29}
{u‘high‘: 179, u‘age‘: 22}
{u‘high‘: 176, u‘age‘: -27}
---------------------多條件查詢
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
----------------IN
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
----------------count
4
----------------條件或
大於等於29或者小於23
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
------------exists用法
age exists {u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
age exists {u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
age exists {u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
age exists {u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
age exists {u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
age exists {u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
age exists {u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
age not exists {u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
正則表達式查詢
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
--------------------使用sort排序(文檔中沒有排序的字段也會打印出來,表示最小)
--------------age 升序
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
--------------age 降序
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
--------------age升序,high升序
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
--------------age升序,high降序
{u‘comment‘: u‘after replace_one operation just exists comment(key)‘, u‘_id‘: 1}
{u‘high‘: 176, u‘comment‘: u‘use update_many‘, u‘age‘: -27, u‘_id‘: 0, u‘name‘: u‘zzzzz‘}
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
{u‘high‘: 179, u‘comment‘: u‘use update_many‘, u‘age‘: 22, u‘_id‘: 5, u‘name‘: u‘zhangjian‘}
{u‘high‘: 173, u‘comment‘: u‘use update_many‘, u‘age‘: 26, u‘_id‘: 2, u‘name‘: u‘zhang‘}
{u‘high‘: 180, u‘comment‘: u‘use update_many‘, u‘age‘: 29, u‘_id‘: 3, u‘name‘: u‘wei‘}
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
-------------$all
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
-------------$in
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
-------------$size用法
-------------size=3
{u‘age‘: 19, u‘_id‘: 100, u‘list‘: [2, 3, 5], u‘name‘: u‘zwj‘}
-------------size=7
{u‘age‘: 19, u‘_id‘: 101, u‘list‘: [1, 2, 3, 4, 5, 6, 7], u‘name‘: u‘zwj‘}
-------------------$unset用法
---before
{u‘high‘: 158, u‘age‘: 30, u‘_id‘: 4, u‘name‘: u‘weijian‘}
---after
{u‘high‘: 158, u‘_id‘: 4, u‘name‘: u‘weijian‘}

python 連接mongodb ,並將EXCEL文檔導入mongodb