I decided to practice with parsers and working with mongodb, but I encountered the following problem.
There is a collection with the goods. For example, take the following documents from it:
{title: 'acer aspire 6420', source: 'amazon', price: 300} {title: 'acer aspire 6420', source: 'ebay', price: 320} Of these, you must obtain the following document:
{title: 'acer aspire 6420', amazon: 300, ebay: 320} that is, group by name and display the price in each of the stores.
During the trial I wrote the following code on python:
from bson.code import Code reducer = Code(""" function(origin, res){ res[origin.source] = origin.price } """) db.products.group(key={"source":1, 'title': 1, 'price': 1}, condition={'title': 'acer aspire 6420'}, initial={}, reduce=reducer) but in the end I get this result:
{title: 'acer aspire 6420', source: 'amazon', price: 300, amazon: 300, ebay: 320} {title: 'acer aspire 6420', source: 'ebay', price: 320, amazon: 300, ebay: 320} Tell me, please, in which direction to dig further or how to implement it, so that the result would be like this:
{title: 'acer aspire 6420', amazon: 300, ebay: 320} I googled and read the documentation, really. I just don’t really understand how to tie it all together correctly.