pymongo
from pymongo import MongoClient
client = MongoClient()
coll = client.db.collections
mongo
- aggregate
group 近似于 groupby
db.collections.aggregate([
{$match:
{'GENDER': 'F',
'DOB':
{ $gte: 19400801,
$lte: 20131231 } } },
{$group:
{_id: "$GENDER",
totalscore:{ $sum: 1}}}
])
- distinct
db.collections.distinct('key1', {'key2':'str chosen'})
# distinct(fields, {query}, {options})
- mongoexport
mongoexport -d database -c collection -f name, age -q '{"raw.category":"首頁>圖解"}' -o data.json
# 篩選fields 且當fields為dict時,有以下兩種寫法卖陵。
# 注意颁督,僅可用于export成csv的情況2⒋濉!!
mongoexport -d database -c collection -f '_id,raw.website,raw.category,raw_doc.metadata.institution' --type csv -o data.csv
mongoexport -d database -c collection -fieldFile files.txt --type csv -o data.csv
files.txt
_id
raw.website
raw.category
raw_doc.metadata.institution
【用于進入mongo之前】
- mongo 正則表達式
db.posts.find({post_text:{$regex:"runoob"}})