1.1连接本地数据库
zxl@zxl:~$ mongo 127.0.0.1 MongoDB shell version: 2.6.10 connecting to: 127.0.0.1/test Server has startup warnings: 2019-09-19T08:21:59.078+0800 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended. 2019-09-19T08:21:59.078+0800 I CONTROL [initandlisten]
1.2查看数据库
> show databases autotest 0.078GB local 0.078GB zxl_test_db 0.203GB
1.3切换数据库
> use zxl_test_db switched to db zxl_test_db
1.4查看数据库中的表
> show tables system.indexes table_10102
1.5一次查询行数
DBQuery.shellBatchSize = 300
1.6普通查询
> db.table_10102.find()
1.7统计查询结果次数
> db.table_10102.find().count() 22835
1.8组合查询
> db.table_10102.aggregate([{"$group": {'_id': {'id': "$id", 'version': "$version", 'ptf': "$ptf"}, 'count': {'$sum': 1}}}])2.python查询
#! /usr/bin/env python# -*- coding: utf-8 -*-import pymongofrom bson.son import SON MONGO_SERVER = "127.0.0.1"MONGO_PORT = 27017conn = pymongo.MongoClient(MONGO_SERVER, MONGO_PORT)db = conn["zxl_test_db"]table_10102 = db['table_10102']if __name__ == "__main__": pipeline = [ # {'$match': {'id': "5"}}, # {'$match': {'version': "2"}}, # {'$match': {'province': "广东省"}}, # {'$match': {'province': []}}, # {"$group": {"_id": "$province", "count": {"$sum": 1}}}, {"$group": {'_id': {'id': "$id", 'version': "$version", 'ptf': "$ptf"}, 'count': {'$sum': 1}}}, # {"$group": {'_id': {'province': "$province", 'city': "$city"}, 'count': {'$sum': 1}}}, # {"$group": {"_id": "$city", "count": {"$sum": 1}}}, # {"$group": {"_id": "$ip", "count": {"$sum": 1}}}, {"$sort": SON([("count", -1), ("_id", -1)])} ] for item in db.table_10102.aggregate(pipeline): print(item)