top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Agreggation in Mongo Data API ...

+1 vote
309 views

I am new in mongodb...

Actually I am learning to use database and chosen MongoDB, for my work I need to use MongoLab Data API. Unfortunately I am not understanding a lot of thinks, for example hot to so and aggregation in DATA API.

Can someone share me any example?

posted Feb 9, 2016 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

There are several methods of performing aggregations in MongoDB. These examples cover the new aggregation framework, using map reduce and using the group method.

Setup
To start, we’ll insert some example data which we can perform aggregations on:

from pymongo import MongoClient
db = MongoClient().aggregation_example
result = db.things.insert_many([{"x": 1, "tags": ["dog", "cat"]},
... {"x": 2, "tags": ["cat"]},
... {"x": 2, "tags": ["mouse", "cat", "dog"]},
... {"x": 3, "tags": []}])
result.inserted_ids
[ObjectId('...'), ObjectId('...'), ObjectId('...'), ObjectId('...')]
Aggregation Framework
This example shows how to use the aggregate() method to use the aggregation framework. We’ll perform a simple aggregation to count the number of occurrences for each tag in the tags array, across the entire collection. To achieve this we need to pass in three operations to the pipeline. First, we need to unwind the tags array, then group by the tags and sum them up, finally we sort by count.

As python dictionaries don’t maintain order you should use SON or collections.OrderedDict where explicit ordering is required eg “$sort”:

Note aggregate requires server version >= 2.1.0.

from bson.son import SON
pipeline = [
... {"$unwind": "$tags"},
... {"$group": {"_id": "$tags", "count": {"$sum": 1}}},
... {"$sort": SON([("count", -1), ("_id", -1)])}
... ]
list(db.things.aggregate(pipeline))
[{u'count': 3, u'_id': u'cat'}, {u'count': 2, u'_id': u'dog'}, {u'count': 1, u'_id': u'mouse'}]
To run an explain plan for this aggregation use the command() method:

db.command('aggregate', 'things', pipeline=pipeline, explain=True)
{u'ok': 1.0, u'stages': [...]}
As well as simple aggregations the aggregation framework provides projection capabilities to reshape the returned data. Using projections and aggregation, you can add computed fields, create new virtual sub-objects, and extract sub-fields into the top-level of results.

See also The full documentation for MongoDB’s aggregation framework
Map/Reduce
Another option for aggregation is to use the map reduce framework. Here we will define map and reduce functions to also count the number of occurrences for each tag in the tags array, across the entire collection.

Our map function just emits a single (key, 1) pair for each tag in the array:

from bson.code import Code
mapper = Code("""
... function () {
... this.tags.forEach(function(z) {
... emit(z, 1);
... });
... }
... """)
The reduce function sums over all of the emitted values for a given key:

reducer = Code("""
... function (key, values) {
... var total = 0;
... for (var i = 0; i < values.length; i++) {
... total += values[i];
... }
... return total;
... }
... """)
Note We can’t just return values.length as the reduce function might be called iteratively on the results of other reduce steps.
Finally, we call map_reduce() and iterate over the result collection:

result = db.things.map_reduce(mapper, reducer, "myresults")
for doc in result.find():
... print doc
...
{u'_id': u'cat', u'value': 3.0}
{u'_id': u'dog', u'value': 2.0}
{u'_id': u'mouse', u'value': 1.0}

answer Feb 9, 2016 by Rajan Paswan
Similar Questions
+2 votes

Considering working with a shard replica-set cluster, some emergency such as re-pick primary node will happen and maybe we can retry the command several times for waiting for recover of mongo.

Is there anyone know when use c driver api such as mongoc_collection_update, mongoc_collection_commond, mongoc_collection_insert, which error codes tell me that I can retry command for making the application more robust.

0 votes

I am using Mongo DB.One of the collection has multiple arrays of json (nested loop) around 200 lines. My Mongo DB is hosted in AWS.
Currently I am creating java script to fetch simple json data in collection. Can you suggest any tool to fetch bulk data collection and implement logic.

+1 vote

I have a Created a Mongo Document inserted successfully to the Collection ~ "urldetls" which has 12 specific Unique keys with data in the values.

Spec: Mongodb 3.x, Java 1.7,

Question: I would like to retrieve the full document based on the value searched within all of the documents.
Problem: I cannot retrieve the document based on the key of the document.

How can i retrieve? I would like to use the Mongodb API 3.x version using java .

0 votes

How could we use encrypted password in connection string? Let suppose if we are using below string:mongo localhost:27017/admin -u abc -p Test123

But we don't want the password to be publically visible directly in connection string.

Is there any way to use encrypted password?

...