我有一个包含集合users
id,...,subscribedFlux
的数据库。每个id
可以包含多个subscribedFlux
,我想知道已订阅了多少Flux
。该查询与Studio3t配合得很好,给出了预期的结果:
当我使用Pymongo时,我什么都得不到:
:~$ python testMongoDB.py
/home/antoine/anaconda2/lib/python2.7/site-packages/pymongo/common.py:555: UserWarning: Unknown option 3t.databases
warnings.warn(str(exc))
/home/antoine/anaconda2/lib/python2.7/site-packages/pymongo/common.py:555: UserWarning: Unknown option 3t.uriVersion
warnings.warn(str(exc))
/home/antoine/anaconda2/lib/python2.7/site-packages/pymongo/common.py:555: UserWarning: Unknown option 3t.connectionMode
warnings.warn(str(exc))
/home/antoine/anaconda2/lib/python2.7/site-packages/pymongo/common.py:555: UserWarning: Unknown option 3t.connection.name
warnings.warn(str(exc))
这是我正在使用的脚本(我隐藏了数据库地址):
#! /usr/bin/python
import pymongo
import datetime
import pprint
from pymongo import MongoClient
client = MongoClient('mongodb://swiper_read:swiper_read@3...ESCONDIDO')
db = MongoClient().aggregation_example
cursor = db.users.aggregate(
[
#{"$match": {"subscribedFlux": { "$exists": "true" }}},
{"$group": {"_id": "$subscribedFlux", "count": {"$sum": 1}}}
]
)
for document in cursor:
print (document)
答案 0 :(得分:1)
您不应该将“未知”选项传递给您的MongoDB URI,就像您目前正在使用3t.databases
,3t.uriVersion
等...而是尝试这样做:
client = MongoClient('mongodb://user:pass@ip:port/') # pass just these options
db = client.DATABASE_NAME # or client['DATABASE_NAME']
cursor = db.TABLE_NAME.aggregate(
[
#{"$match": {"subscribedFlux": { "$exists": "true" }}},
{"$group": {"_id": "$subscribedFlux", "count": {"$sum": 1}}}
]
)