我正在尝试在 pymongo 中执行等效的 SQL Join,如下所示: http ://blog.knoldus.com/2013/02/03/joins-now-possible-in-mongodb/
问题是,我被卡住了,因为 bson 无法编码集合对象。
bson.errors.InvalidDocument: Cannot encode object: Collection(Database(MongoClient('localhost', 27017), 'Application'), 'Products')
所有相关代码:
class Delivery(Repository):
COLLECTION_ATTRIBUTE = 'deliveriesCollection'
def __init__(self):
Repository.__init__(self, self.COLLECTION_ATTRIBUTE)
def printTable(self):
from bson.code import Code
mapper = Code('function() {'
' product = ProductCollection.findOne({_id:this.Product_ID});'
' data = {'
' \'Name\':this.Name,'
' \'Product_ID\': product.ID'
' };'
' emit(this._id, data );'
'}', ProductCollection = product.collection)
reducer = Code('function(key, values) {'
' return values[0];'
'}')
result = self.collection.map_reduce(mapper, reducer, "myresults")
for doc in result.find():
print(doc)
delivery = Delivery()
product = Product()