如何从pymongo进行正确的查询

时间:2019-02-20 19:17:51

标签: mongodb pymongo

我正在使用pymongo在mongodb中对集合进行子集化,查询语法存在问题,这是我的查询:

import pymongo
client=pymongo.MongoClient("localhost",27017)
db=client("publications")
collec=db("zikaVirus2")

pipeline=db.collec.aggregate([
   {"$match":{"fullText":{"$exists": "true"}}},
   {"$out": "pub_fulltext"}
])

db.pub_fulltext.aggregate(pipeline)

我收到此错误:

Traceback (most recent call last):
  File "genColFulltext.py", line 3, in <module>
    db=client("publications")
TypeError: 'MongoClient' object is not callable
  

更新1

如果我愿意

import pymongo
client=pymongo.MongoClient("localhost",27017)
db=client.publications
collec=db.zikaVirus2

pipeline=db.collec.aggregate([
{"$match":{"fullText":{"$exists": "true"}}},
{"$out": "pub_fulltext"}
])

db.pub_fulltext.aggregate(pipeline)

我明白了

Traceback (most recent call last):
  File "genColFulltext.py", line 11, in <module>
    db.pub_fulltext.aggregate(pipeline)
  File "/home/danielo/.local/lib/python3.5/site-packages/pymongo    /collection.py", line 2397, in aggregate
**kwargs)
  File "/home/danielo/.local/lib/python3.5/site-packages/pymongo/collection.py", line 2242, in _aggregate
    common.validate_list('pipeline', pipeline)
  File "/home/danielo/.local/lib/python3.5/site-packages/pymongo/common.py", line 428, in validate_list
    raise TypeError("%s must be a list" % (option,))
TypeError: pipeline must be a list
  

更新2

这段代码运行没有问题,但是却没有达到他的目的,集合pub_fulltext在应该包含数据的情况下也没有任何东西。

import pymongo
client=pymongo.MongoClient("localhost",27017)
db=client.publications
collec=db.zikaVirus2

pipeline=db.collec.aggregate([
{"$match":{"fullText":{"$exists": "true"}}},
{"$out": "pub_fulltext"}
])
for item in pipeline:
    db.pub_fulltext.aggregate(item)

然后我尝试一下:

import pymongo
client=pymongo.MongoClient("localhost",27017)
db=client.publications
collec=db.zikaVirus2

pipeline=[db.collec.aggregate([
{"$match":{"fullText":{"$exists": "true"}}},
{"$out": "pub_fulltext"}
])]
for item in db.collection.aggregate(pipeline):
    db.collection.aggregate(item)

出现此错误:

Traceback (most recent call last):
  File "genColFulltext.py", line 10, in <module>
    for item in collection.aggregate(pipeline):

Traceback (most recent call last):   File "genColFulltext.py", line 10, in <module>
for item in db.collection.aggregate(pipeline):   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/collection.py", line 2397, in aggregate
**kwargs)   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/collection.py", line 2304, in _aggregate
client=self.__database.client)   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/pool.py", line 584, in command
self._raise_connection_failure(error)   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/pool.py", line 745, in _raise_connection_failure
raise error   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/pool.py", line 579, in command
unacknowledged=unacknowledged)   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/network.py", line 114, in command
codec_options, ctx=compression_ctx)   File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pymongo/message.py", line 679, in _op_msg
flags, command, identifier, docs, check_keys, opts) bson.errors.InvalidDocument: Cannot encode object: <pymongo.command_cursor.CommandCursor object at 0x1008e4198>

0 个答案:

没有答案