python 连接mongodb ,并将EXCEL文档导入mongodb

一、下载软件

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: 173use 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‘}----------------count4----------------条件或大于等于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‘}
时间: 2024-12-10 09:26:39

python 连接mongodb ,并将EXCEL文档导入mongodb的相关文章

Navicat导入TXT文档到Mysql, excel文档导入用同样操作

1.先做TXT文档的格式工作,另存为UTF-8的txt格式. 然后再文档最顶端,命名跟数据库一样的列名,方便后面识别 2.点击导入 3.选择txt导入向导 4.选择txt文件的位置 5.这里如果是用tab隔开的数据,如图,就不用动,全部默认 6.因为我们前面使用了跟数据库列名一样的名字,这里也不用动,自己可以识别,默认. 7.这里选择数据库的表名,一般识别的都是对的,不对,自己更改一下,想导入哪个数据表中. 8.起一样的列名的好处,就是自己识别,这里默认出来了,如果不对,自己修改. 9.选择模式

Python处理Excel文档(xlrd, xlwt, xlutils)

简介 xlrd,xlwt和xlutils是用Python处理Excel文档(*.xls)的高效率工具.其中,xlrd只能读取xls,xlwt只能新建xls(不可以修改),xlutils能将xlrd.Book转为xlwt.Workbook,从而得以在现有xls的基础上修改数据,并创建一个新的xls,实现修改. (以下属性或方法并非全部,需要更多属性请参看文档:建议先参考文末Demo,再深入了解) xlrd Book(class) 由xlrd.open_work("example.xls"

MongoDB改动、删除文档的域属性实例

MongoDB改动.删除文档的域属性实例 在站点的开发中,可能最初的设计不合理.或者后期业务的变更,会造成文档结构会有些无用的属性.须要去删除或改动.因为MongoDB 是无 Schema 的,不像关系数据库那样列属性定义在表而非记录中,MongoDB 的集合中的每一个文档能够拥有各自不同的域属性.MongoDB 中使用 db.collections.update 改动集合中若干文档的域属性,使用 $set 添加域.$unset 删除域. 删除集合中全部文档的一个域 db.posts.updat

MongoDB,无模式文档型数据库简介

MongoDB的名字源自一个形容词humongous(巨大无比的),在向上扩展和快速处理大数据量方面,它会损失一些精度,在旧金山举行的MondoDB大会上,Merriman说:“你不适宜用它来处理复杂的金融事务,如证券交易,数据的一致性可能无法得到保证”.若想了解更多关于MongoDB的信息,请看51CTO数据库频道推荐<MongoDB,无模式文档型数据库简介>. NoSQL数据库都被贴上不同用途的标签,如MongoDB和CouchDB都是面向文档的数据库,但这并不意味着它们可以象JSON(J

Python3正则表达式清洗Excel文档

本项目中虽然数据量不大,用Excel自带的替换功能也能实现,但是针对上几千条字段去匹配数据的话,Python就明显高效的多,现在开始讲解: 要清洗的是Excel文档中所有字段的地名, 需要清洗数据: 首先,需要导入xlrd和re包,前者是用来读写Excel文档,后者是正则表达式的包 1 #-*- coding:gbk -*- 2 import xlrd 3 import re 4 5 #打开一个Excel表 6 data = xlrd.open_workbook('/home/kin/compa

Go 语言读写 Excel 文档

Excelize 是 Golang 编写的一个用来操作 Office Excel 文档类库,基于微软的 Office OpenXML 标准.可以使用它来读取.写入 XLSX 文件.相比较其他的开源类库,Excelize 支持写入原本带有图片(表)的文档,还支持向 Excel 中插入图片,并且在保存后不会丢失图表样式. 项目主页 github.com/Luxurioust/excelize 安装 go get github.com/Luxurioust/excelize 创建 XLSX packa

java读取WORD/EXCEL模板转换生成新WORD/EXCEL文档

原文:java读取WORD/EXCEL模板转换生成新WORD/EXCEL文档 代码下载地址:http://www.zuidaima.com/share/1550463239670784.htm 可以通过预先设置指定的excel和word模板,通过替换文档里面指定的标志来生成新的excel和word文档.excel的部分只是实现了简单的方法.word部分可以支持word2003和word2007格式.建议word使用07及其以上. 其实excel部分标签和jstl很像,而且支持循环等.word就支

支持将数据导出到Excel文档的时候设置单元格格式的.NET控件Spire.DataExport

Spire.DataExport for .NET是e-iceblue公司推出的一款数据导出类.NET控件.作为一款专业的数据导出控件,Spire.DataExport for .NET可以帮助开发人员轻松快速的从各种主流数据库中导出数据并存储于各种文件格式中.他支持从SQL Command, DataTable,ListView中导出数据并存储于MS Excel,MS Word, HTML, XML, PDF, MS Access, DBF, SQL Script, SYLK, DIF, CS

EXCEL基础内容学习笔记(二)Excel文档的基本组成与功能介绍

一.基本组成 (一)工作簿.工作表与单元格 (1)工作簿:一个Excel文档即为一个工作簿. (2)工作表:工作簿中的每个表. (3)单元格:打开Excel文档,在工作表中单击,出现的加粗四边形即为单元格.单元格由行和列组成,命名时由行和列说明,称为单元格名称或地址. 一个工作簿中有若干个工作表,每个工作表有许多单元格组成. 二.功能介绍 (1)标题栏:Excel文档最上端. (2)选项卡 (3)工作区:每一个打开的选项卡都含有若干工作区. (4)名称框:所选择的单元格的名字. (5)编辑栏:可