Python模块之pymongo使用

1. 安装

pip install pymongo

2. 快速示例


import pymongo

Pymongo基本介绍

连接到MongoDB

数据库(Databases)与集合(Collections)

文档(Documents)

Pymongo CRUD

Pymongo新增文档

Pymongo文档更新

Pymongo文档删除

Pymongo文档常用查询

  1. find
  2. count
  3. etc.

$or 查询

find({"$or":[{"name":"test"},{"age":{"$gt":25}}]})

You can do with regex which does not contain word. Also you can use $options => i for case insensitive search

Doesn't Contains string

db.collection.find({name:{'$regex' : '^((?!string).)*$', '$options' : 'i'}})

Exact case insensitive string

db.collection.find({name:{'$regex' : '^string$', '$options' : 'i'}})

Start with string

db.collection.find({name:{'$regex' : '^string', '$options' : 'i'}})

End with string

db.collection.find({name:{'$regex' : 'string$', '$options' : 'i'}})

Contains string

db.collection.find({name:{'$regex' : 'string', '$options' : 'i'}})

Using re

You need to compile the regex with the re module:

import re
rgx = re.compile('.*IP.*', re.IGNORECASE)  # compile the regex
cursor = db.tweets.find({'text':rgx},{'text':1,'created_at':1})

You can use re.IGNORECASE as flag if you want to match iP, Ip and ip as well. If you do not want that, you can drop the re.IGNORECASE part.



评论(0条)

暂时还没有评论,第一个来评论吧!


我要发表看法

引用   粗体   链接   缩进  

最近编辑

热门标签