FAQ Database Discussion Community


python mongodb $match and $group

python,mongodb,aggregation-framework
I want to write a simple query that gives me the user with the most followers that has the timezone brazil and has tweeted 100 or more times: this is my line : pipeline = [{'$match':{"user.statuses_count":{"$gt":99},"user.time_zone":"Brasilia"}}, {"$group":{"_id": "$user.followers_count","count" :{"$sum":1}}}, {"$sort":{"count":-1}} ] I adapted it from a practice problem. This was...

Model.aggregate gives me different results as in the mongo console (none for mongoose)

node.js,mongodb,mongoose,mongodb-query,aggregation-framework
When I execute the following in the console I get one nameID and a min max. db.prices.aggregate([ {"$match":{ nameID: { '$in': [ ObjectId('longid') ] } } }//replaced the actual value with longid ,{"$group":{"_id":"$nameID","nameID":{"$first": "$nameID"} ,"min":{"$min":"$price"},"max":{"$max":"$price"}}} ,{"$sort":{"min":1}} ,{"$limit":5} ]) When in mongoose I get an empty array: Price.aggregate([ {"$match":{ nameID: {...

Mongodb group by dbref field

mongodb,mapreduce,aggregation-framework
I need group products by model. Each product has model field - DBRef to Models collection. I tried use this aggregate query, but have error FieldPath field names may not start with '$'. Aggregation query: db.Products.aggregate([ { $project: { _id: 0, model: 1, isActive: 1 } }, { $group: {...

MongoDB counts multiple fields

mongodb,spring-data,aggregation-framework,spring-mongo
Here is the input collection : {"itemName" : "A1", "voteType" : "up"} {"itemName" : "A1", "voteType" : "up"} {"itemName" : "A1", "voteType" : "down"} {"itemName" : "A2", "voteType" : "up"} {"itemName" : "A2", "voteType" : "down"} {"itemName" : "A2", "voteType" : "down"} {"itemName" : "A2", "voteType" : "down"} and so...

How could I remove the duplicated items(complex object) from array

mongodb,aggregation-framework
In each document, the records is an array containing many duplicated objects. and in buy_items there are also containing many duplicated items. How could I clean the duplicated items ? Original documents: { "_id": "0005d116qwwewdq82a1b84f148fa6027d429f3e", "records": [ { "DATE": new Date("1996-02-08T08:00:00+0800"), "buy_items": [ "5210 ", "5210 ", "5210 " ]...

MongoDB server-side aggregate queries shared between multiple scripts

php,python,mongodb,aggregation-framework
We have a MongoDB database from which we pull data from different sources, using the aggregate framework. A python script generates pdf reports, our dashboard plots graphs of data pulled by the php backend. These scripts use mostly the same aggregate pipelines, but the code is replicated in python and...

Order_by length of listfield in mongoengine

mongodb,python-2.7,mongodb-query,aggregation-framework,mongoengine
I wan't to run a query to get all Articles that have more than 6 com and then sort according length of com list, for this i doing it: ArticleModel.objects.filter(com__6__exists=True).order_by('-com.length')[:50] suppose com is a ListField, but ordering not work, how can i fix it? thanks...

is It possible to compare two Months Data in single Collection in MongoDB?

mongodb,mongodb-query,aggregation-framework
I have collection database with 10 000 000 call records. I want to compare call usage of previous month to next month. Example of collection document { "_id" : ObjectId("54ed74d76c68d23af73e230a"), "msisdn" : "9818441000", "callType" : "ISD" "duration" : 10.109999656677246, "charges" : 200, "traffic" : "Voice", "Date" : ISODate("2014-01-05T19:51:01.928Z") } {...

Find all records based on groupby, count and max createDate

mongodb,aggregation-framework
I would like to find all records based on groupby, count and the detail of whose createDate is maximum in same document, and the following is a sample collection: /* 0 */ { "_id" : 1, "name" : "pradeep", "age" : 26, "createDate" : ISODate("2015-06-20T22:14:41.423-18:30") } /* 1 */ {...

What is mongo $mergeCursors?

mongodb,aggregation-framework
We see a lot of slow queries in mongo logs like below (with pipeline op mergeCursors). We have a shaded mongo with 2 shards with only primaries. What is mergeCursors command? Please let me know if any other information is required. { "_id" : ObjectId("5571b739f65f7e64bb806362"), "op" : "command", "ns" :...

MongoDB Aggregation - match if value in array

mongodb,aggregation-framework
I have a collection that I'm performing an aggregation on and I've basically gotten it down to {array:[1,2,3], value: 1}, {array:[1,2,3], value: 4} How would I perform an aggregation match to check if the value is in the array? I tried using {$match: {"array: {$in: ["$value"]}}} but it doesn't find...

Array of embedded documents - MongoDB

mongodb,aggregation-framework
I have these two MongoDB documents. I want to find the capacity of Real and Barcelona stadiums, supposing Real Madrid has two stadiums(sorry Merengues :) { "_id" : "Bar.43", "official_name" : "Futbol Club Barcelona "country" : "Spain", "started_by" : { "day" : 28, "month" : 11, "year" : 1899 },...

Adding an array field to a collection based on values of another array field in mongodb

arrays,mongodb,mongodb-query,aggregation-framework
I have a collection which has the following structure as denoted by one document { _id : 1, array1 : [{fld1 : 'doc1e1fld1', fld2: 'doc1e1fld2'},{fld1:'doc1e2fld1',fld2: 'doc1e2fld2'}] } I would like to add another field to ALL elements in the collection and set the value such that the modified doc above...

How to compare grouped data between two dates using the mongodb aggregate framework

javascript,mongodb,aggregation-framework,aggregation
Consider a data set as follows Sector Value Date a 2 1/1/2015 b 5 1/1/2015 c 8 1/1/2015 a 3 1/1/2015 a 6 1/1/2015 a 1 1/1/2015 c 1 1/1/2015 Sector Value Date a 2 2/1/2015 b 10 2/1/2015 b 5 2/1/2015 c 8 2/1/2015 Is there a way to...

MongoDB perl driver : different result after aggregation

perl,mongodb,aggregation-framework
I have collection 'books': { "_id" : ObjectId("54901b078b823f2014080700"), "title" : "title1", "available" : 0, "time" : 14318789245, "score" : 1, "categories" : ["fiction","humor"] }, { "_id" : ObjectId("54901b078b823f2014080701"), "title" : "title2", "available" : 1, "time" : 14318789745, "score" : 1, "score_fiction": 5, "categories" : ["fiction",] } ... { "_id" :...

mongodb aggregation: group by _id

json,mongodb,mongoose,aggregate,aggregation-framework
After aggregation I got this result. The _id field is each class's id and I applied {$group:{_id:"$_id", ..... [{ _id: 54c977314f5293b74ea54f96, subject: 'math' }, score: 73.5335 }, { _id: 54c977314f5293b74ea54f96, subject: 'science' }, score: 56.2192 }, { _id: 54c977314f5293b74ea54f96, subject: 'history' }, score: 82.8821 }, { _id: 54c974bdff0d993b4ecf34ce, subject: 'math'...

How to remove duplicate values inside a list in mongodb

mongodb,mongodb-query,pymongo,aggregation-framework
I have a mongodb collection . When I do. db.bill.find({}) I get, { "_id" : ObjectId("55695ea145e8a960bef8b87a"), "name" : "ABC. Net", "code" : "1-98tfv", "abbreviation" : "ABC", "bill_codes" : [ 190215, 44124, 190215, 147708 ], "customer_name" : "abc" } I need an operation to remove the duplicate values from the bill_codes....

MongoDB elemmatch multiple elements in array

mongodb,mongodb-query,aggregation-framework
I have an mongodb document like { "_id" : ObjectId("54e66b2da7b5f3a92e09dc6c"), "SomeMetric" : [ { //some object } { //some object } ], "FilterMetric" : [ { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class" : "s1" }, { "min" : "0.00", "max" : "16.83", "avg" : "0.00", "class"...

Mongoose error on aggregate group

node.js,mongodb,mongoose,mongodb-query,aggregation-framework
i have this model: var Chat = new Schema({ from: String, to: String, satopId: String, createdAt: Date }); var Chat = mongoose.model('Chat', Chat); I want do a query to do a query that returns the max created at grouping by to and from field. I tried with: Chat.aggregate([ { $group:...

How can i push values of a specific field with mongoose aggregate

javascript,node.js,mongodb,mongodb-query,aggregation-framework
I am using this code inside a route: MyModel.aggregate( [ { "$group": { "_id": "$date", "participants": { "$sum": "$participants" }, "peoples": { $sum: 1 } } }, ], function(err, result) { console.log(result) } ......etc...... This is aggregating the date, participants and peoples, but i want access the name field, and...

How do I append Mongo DB aggregation results to an existing collection?

mongodb,aggregation-framework
I am trying to perform several insertions on an existent Mongo DB collection using the following code db.dados_meteo.aggregate( [ { $match : { "POM" : "AguiardaBeira" } }, { $project : { _id : { $concat: [ "0001:", { $substr: [ "$DTM", 0, 4 ] }, { $substr: [ "$DTM",...

MongoDb aggregation framework value of a field where max another field

mongodb,aggregation-framework
I have a collection that has records looking like this: "_id" : ObjectId("550424ef2f44472856286d56"), "accountId" : "123", "contactOperations" : [ { "contactId" : "1", "operation" : 1, "date" : 500 }, { "contactId" : "1", "operation" : 2, "date" : 501 }, { "contactId" : "2", "operation" : 1, "date" :...

Count multiple fields in MongoDB using python

python,mongodb,aggregation-framework
I have this simple mongodb query: db.checks_reports_data_df8.find({"report_id": str(report["_id"])}) The collection checks_reports_data_df8 is having some fields like "success" (can be 0 or 1) and is_listed (can be 0 or 1). What I need is to be able to get the counts of all fields where success = 1 as count_success and...

Group and Match aggregrate MongoDB

mongodb,aggregate,aggregation-framework
I have a documents like this: { u '_id': ObjectId('5534cd32e4b0d5f14e6aa27d'), u 'geoip': { u 'coordinates': [-96.8353, 32.9299 ], u 'region_name': u 'TX', u 'latitude': 32.9299, u 'ip': u '173.193.154.240', u 'area_code': 972, u 'continent_code': u 'NA', u 'country_code3': u 'USA', u 'country_code2': u 'US', u 'city_name': u 'Dallas', u 'longitude':...

Aggregation working in console but not node?

javascript,node.js,mongodb,aggregation-framework
I am totally new to node.js (and Javascript too), so the idea of anonymous functions, callbacks and the likes is a totally foreign to me and I'm still wrapping my head around it all... so that said please go easy on me! :) I've got the following query I'm able...

Average of value fields of documents with id higher than 10

node.js,mongodb,mongodb-query,aggregation-framework
I'm using express, nodejs, monk. collection consists of objects: {_id, valueField} Let's assume that _id is overridden on insert and the id's of 5 documents in the collection range from 1 to 5. They are all integers. This is what I'm trying now - but I believe the syntax is...

Nothing happens when trying to use $project

python,mongodb,aggregation-framework
I am new to mongodb and still sitting on the same pipeline thing. I dont understand why my usage of $project did not generate any output at all ? def make_pipeline(): # complete the aggregation pipeline pipeline = [ { '$match': { "user.statuses_count": {"$gt":99 }, "user.time_zone": "Brasilia" } }, {...

Mongoose Virtuals in MongoDB Aggregate

node.js,mongodb,mongoose,aggregation-framework
My Mongoose Schema is as follows: var DSchema = new mongoose.Schema({ original_y: {type: Number},, new_y: {type: Number},, date: {type: Date}, dummy: [dummyEmbeddedDocuments] }, toObject: { virtuals: true }, toJSON: { virtuals: true} }); DSchema.virtual('dateformatted').get(function () { return moment(this.date).format('YYYY-MM-DD HH:mm:ss'); }); module.exports = mongoose.model('D', DSchema); A document in my schema would...

Mongo aggregation, accessing a portion of an array

mongodb,aggregation-framework
I have a mongo document like this { '_id': .bson. 'key': [ 'word1', 'word2', { 'key1': 'value1', 'key2': 'value2' } ] }, How do I use the mongo aggregation framework to make the document look like this: { '_id': .bson. 'key2': 'value2' }, Thank you in advance! -daniel...

MongoDB Text search fails for stop words

mongodb,aggregation-framework
I'm trying to do a query in my collection, but its not returning anything. Here's my query: {'$match': {'$text': {'$search': 'a'}}}, {'$group': {'_id': {'texto': '$texto'}, 'somanumero': {'$sum': '$numero'}}} My collection: { "_id" : ObjectId("555cdc4fe13823315537042d"), "texto" : ObjectId("555cdc4fe13823315537042c"), "numero" : ObjectId("555cdc4fe13823315537042e") } { "_id" : ObjectId("555cdc5ee13823315537042f"), "numero" : 5, "texto" :...

MongoDB: Project using array fields

mongodb,mongodb-query,aggregation-framework
I have a collection like this: Each document contains Message field which in turn contains array of Fields. Each document underneath Fields array has Value and Name properties. [ { "_id" : ObjectId("55711efed0103b1598140076"), "Message" : { "Fields" : [ { "Value" : 131, "Name" : "Options", }, { "Value" :...

how to project grouping into object in mongodb?

mongodb,aggregation-framework
I'm trying to do a simple GROUP BY/COUNT in mongodb with this aggregation query: { $match:{ 'os':7 } }, { $group : { _id:"$status", count:{$sum:1} } }, { $project:{ count:1, status:1 } } It's return an array like this [{ "_id" : "ENC", "count" : 18 } { "_id" :...

Show only an element found in an array in mongodb

arrays,mongodb,aggregation-framework
Hello: I have this collection: /* 1 */ { "_id" : ObjectId("553e316e39108e802a1edeb9"), "coleccion" : "aplicaciones", "nombre" : "otra aplicacionn", "descripcion" : "w aplicacion", "tipo" : "privada", "campoId" : [ { "id" : "1430230303", "id_campo" : "553bffed39108e346ccff7ab", "orden" : "25", "obligatorio" : "0" }, { "id" : "1430233750", "id_campo" : "553bffca39108eb163cff7aa"...

count multiple distinct fields by group with Mongo

mongodb,aggregation-framework
I have a data set looks as {"BrandId":"a","SessionId":100,"UserName":"tom"} {"BrandId":"a","SessionId":200,"UserName":"tom"} {"BrandId":"b","SessionId":300,"UserName":"mike"} I would like to count distinct session and username group by brandid, the sample sql is like: select brandid,count_distinct(sessionid),count_distinct(username) from data group by brandid I tried to write Mongo DB, my current code is as following and it does not...

How to group by different fields

mongodb,aggregation-framework
I want to find all users named 'Hans' and aggregate their 'age' and number of 'childs' by grouping them. Assuming I have following in my database 'users'. { "_id" : "01", "user" : "Hans", "age" : "50" "childs" : "2" } { "_id" : "02", "user" : "Hans", "age" :...

how do i apply $match, $group and $sum in single query using mongoose/mongodb

node.js,mongodb,mongoose,aggregation-framework
I am trying to find solution with my basic knowledge of MongoDB. I have data like below { _id: 45556, "name": "John", "gender": "male", "lunch_preference":[ {"outlet":"KFC", "day":"monday", "spent":300}, {"outlet":"Mc", "day":"friday", "spent":250}, {"outlet":"dominos", "day":"sunday", "spent":400} ] } { _id: ab123, "name": "Peter", "gender": "male", "lunch_preference":[ {"outlet":"dominos", "day":"tuesday", "spent":150}, { "outlet":"Mc", "day":"wednesday",...

MongoDB count objects inside objects

mongodb,mapreduce,mongodb-query,aggregation-framework
i want to know how many elements there are inside objects. My collections is the following: {"_id": 'xxx', "comments":{ "_id_ab6666djdkjd": { "text": "my text" }, "_id_ab6dee": { "text": "my text2" } } }, {"_id": 'xxx', "comments":{ "_id_ab6666dddjdkjd": { "text": "my text" } } } I want to count comments in...

Aggregate data stored as time_t in MongoDB

mongodb,mongoose,aggregation-framework
I have a collection of documents stored in MongoDB which include a created field as a unix style time_t: e.g. {"created": 1137459055216, "qty": 3 } How can I aggregate this data by month through a query?...

How to count incomming links?

mongodb,aggregation-framework
I have documents looking like this: { "url" : "http://example.com", "links" : [ "http://example.com/foo", "http://example.com/bar" ] }, { "url" : "http://example.com/foo", "links" : [ "http://example.com/bar" ] }, { "url" : "http://example.com/lost", "links" : [ "http://example.com/bar" ] }, { "url" : "http://example.com/bar", "links" : [] } How to aggregate by url...

Aggregate to get average from document and of array elements

mongodb,mongoose,mongodb-query,aggregation-framework
I've got this array of these objects: item1 = { name:'item', val:1, list:[ {type:'a',value:1}, {type:'b',value:1}, {type:'c',value:1} ] }; item2 = { name:'item', val:5, list:[ {type:'a',value:3}, {type:'b',value:99}, {type:'c',value:1} ] }; They all have the same array, same types 'a', 'b' & 'c', but different values. How can I get the average...

MongoDB : querying documents with two equal fields, $match and $eq

database,node.js,mongodb,npm,aggregation-framework
What is the best way to return all documents in a collection if I want document.a == document.b? I've tried db.collection.aggregate([ { $match: { $eq: [ '$a', '$b' ] } }]) But it returns with no errors or results, because I assume it is literally matching strings "$a" and "$b"....

MongoDB sort documents by array elements

mongodb,sorting,mongodb-query,aggregation-framework
I have document structure like : { map: 'A', points: [ { type: 'type1', distanceToSpawn: 110 }, { type: 'type4', distanceToSpawn: 40 }, { type: 'type6', distanceToSpawn: 30 } ] }, { map: 'B', points: [ { type: 'type1', distanceToSpawn: 100 }, { type: 'type2', distanceToSpawn: 60 }, { type:...

grouping data in mongo using json data stored

mongodb,grouping,aggregate-functions,aggregation-framework
This is the data I have in my mongo db: { "_id": ObjectId("556d1c7716efd4a035d8e473"), "products": [ { "gtin": 77770000222313, "gpc": 10000068 }, { "gtin": 77770000222312, "gpc": 10000068 } ] } How do I aggregate this so that I get gpc value and then an array under that of the gtins? Something...

MongoDB - Increase nested values in Object

mongodb,mongodb-query,aggregation-framework
Please consider "group" collection following document "auto_availability" : { "54c5c59d75de3e8d0a8b4567" : 12, "54c5c59d75de3e8d0a8b4568" : 12, "54c5c59d75de3e8d0a8b4569" : 12, "54c5c59d75de3e8d0a8b456a" : 12, "54c5c59d75de3e8d0a8b456b" : 12, "54c5c59d75de3e8d0a8b456c" : 12, "54c5c59d75de3e8d0a8b456d" : 12, "54c5c59d75de3e8d0a8b456e" : 12, "54c5c59d75de3e8d0a8b456f" : 12, "54c5c59d75de3e8d0a8b4570" : 12, "54c5c59d75de3e8d0a8b4571" : 12, "54c5c59d75de3e8d0a8b4572" : 12, "54c5c59d75de3e8d0a8b4573" : 12, "54c5c59d75de3e8d0a8b4574" :...

MongoDB aggregate objects with an array of objects?

mongodb,aggregation-framework
Here is a part of my collection in json: [{"company":"Lenovo", "models": [{"model":"ThinkPad T400", "gens": [{"gen":"3", "start":2012}, {"gen":"2", "start":2008}, {"gen":"1", "start":2004}] }...] }...] I am trying to come-up with an aggregate command that will return me the following: [{ "_id":"Lenovo", "models":[{ "model": "ThinkPad T400", "gens":["1","2","3"] }...] }...] I have tried the...

Concatenate string values in array in a single field in MongoDB

arrays,mongodb,mapreduce,mongodb-query,aggregation-framework
Suppose that I have a serie of documents with the following format: { "_id": "3_0", "values": ["1", "2"] } and I would want to obtain a projection of the values in the array concatenated in a single field. I would want to obtain each document with the following format. {...

Mongodb: Making a field the result of an operation in a query

mongodb,mongodb-query,aggregation-framework
How to include fields operations in query with MongoDB ? My documents contain a 'start_time' and a 'duration' fields, and I want get all documents that are style active (where 'now' is lower than 'start_time' + 'duration'). The PostgreSQL equivalent is : SELECT * FROM my_table WHERE start_time + (INTERVAL...

MongoDB aggregation: Project separate document fields into a single array field

mongodb,mongodb-query,aggregation-framework
I have a document like this: {fax: '8135551234', cellphone: '8134441234'} Is there a way to project (without a group stage) this document into this: { phones: [{ type: 'fax', number: '8135551234' }, { type: 'cellphone', number: '8134441234' }] } I could probably use a group stage operator for this, but...

Mongodb use aggregate to iterate through collection

regex,mongodb,for-loop,aggregation-framework
I have a collection of summaries called collectioname, and an list of key words called keywords. I used below code filter out all the summaries in collectioname that contains the first key word in the list, and it worked great. db.collectioname.aggregate([ { $match: { summary: {$regex: keywords[1], $options:"i"} } },...

MongoDb Access array of objects with certain property

arrays,mongodb,mongodb-query,aggregation-framework
I have one document as follows: { user: 'hvt07', photos: [ { link: 'http://link.to.com/image1.jpg', isPrivate: true }, { link: 'http://link.to.com/image2.jpg', isPrivate: false } ] } I want to get all photos which are with: isPrivate: false I am using the following query: db.collection_name.find({ photos:{ $elemMatch:{isPrivate: false} } }).pretty() I have...

mongodb aggregate unwind array and no array

arrays,mongodb,aggregation-framework
I'm trying to write an aggregate query using $unwind no matter the element is an array or not. I know $unwind does not work on no array elements, but I wonder if there is a way to make it work, like converting this element into an array. I have a...

How do i count number of referals using aggregation in mongoDB

mongodb,mongodb-query,aggregation-framework
this is the schema : { "_id" : ObjectId("54f8d7ad92ccf803008a0e4f"), "personal" : { "name" : "test", "placa" : "BBB222" }, "recruiter" : { "user_id" : "541cba6fe4b0288d56081fe2", "date" : 1425594285410, "name" : "Mario Hart", "rol" : "greeter", "channel" : "referido", "referred" : "VERA" }, I want to create a list of names...

MongoDb aggregation-framework, join different documents are in the same collection

mongodb,mongodb-query,aggregation-framework
I want to help get mongodb one of several documents. I have the following data structure: db.coll.insert([ {_id: "gomer", doc_type: "user", group: ["user", "author"] }, {_id: "dante", doc_type: "user", group: ["user"] }, {_id: 1, doc_type: "blogs", user: "gomer", article: "aaaa" }, {_id: 2, doc_type: "blogs", user: "vasya", article: "bbbb" }...

Chronological ordering with the $week operator

mongodb,aggregation-framework
The $week operator works great when you're grouping documents by dates within a week. However, is there a sort operator that can sort the document chronologically rather than numerically? I'd like to sort documents in such a way that it is easy to see grouping as they cross over the...

Find oldest/youngest post in mongodb collection

mongodb,datetime,aggregation-framework,datetime-format
I have a mongodb collection with many fields. One field is 'date_time', which is in an ISO datetime format, Ex: ISODate("2014-06-11T19:16:46Z"), and another field is 'name'. Given a name, how do I find out the oldest/youngest post in the collection? Ex: If there are two posts in the collection 'data'...

how can I find the average of the sums of a field?

mongodb,aggregation-framework
I have documents that looks like this { parentId: 2, childId: 4, data: 7 }, { parentId: 2, childId: 3, data: 5 }, { parentId: 2, childId: 3, data: 1 } I would like to pull the average of the sum of the data grouped by the childId where the...

get the fields in the input document after $group in aggregation pipeline in MongoDB

mongodb,mongodb-query,aggregation-framework
The problem I am facing is that how to access the original document after one group operation and also carry the fields after $group in aggregation pipeline in MongoDB. For example: [ group, unwind, group] Original document is: { "_id" : ObjectId("361de42f1938e89b179dda42"), "user_id" : ObjectId("9424021bafbde55512e39b83"), "candidate_id" : ObjectId("54f65356294160421ead3ca1") "OVERALL_SCORE" :...

Mongoose aggregation not sorting dates

javascript,node.js,mongodb,mongoose,aggregation-framework
Can anyone help me understand why this aggregation query is not sorting the results by their created_at date? User.aggregate([ // Get only records created in the last 30 days { $match: { "created_at":{ $gt: new Date(today.getTime() - 1000*60*60*24*30) } }}, //Sort the results by signup date { $sort : {...

Keep the main document when filtered array is empty in aggregation pipeline

mongodb,mongoose,mongodb-query,aggregation-framework
This is really an addition to a previously asked question. With some help from @JohnnyHK I can now remove unwanted subdocuments from an array based on a particular criteria: deleted != null. I discovered that my $unwind pipe broke when there were no items in the array so I also...

PHP & MongoDB show results grouped by date

php,mongodb,aggregation-framework
I have an mongodb collection with following documents: { "_id" : ObjectId("547af6aea3f0eba7148b4567"), "check_id" : "f5d654e7-257d-4a93-ae50-2d59dfeeb451", "chunks" : NumberLong(200), "num_hosts" : NumberLong(1000), "num_rbls" : NumberLong(163), "owner" : NumberLong(7901), "created" : ISODate("2014-11-30T10:51:26.924Z"), "started" : ISODate("2014-11-30T10:51:31.558Z"), "finished" : ISODate("2014-11-30T10:57:08.512Z") } { "_id" : ObjectId("54db19a858a5d395a18b4567"), "check_id" : "9660e510-1349-43f3-9d5e-8bf4b06179be", "chunks" :...

mongodb aggregate users count by date

ruby,mongodb,aggregation-framework
I'm trying to group all users by some date, what I want is name of month and number of users, month will be a variable so it can change to year or week. Here is my code: logs = user_collection.aggregate([ # Lets find our records {"$match" => {"zip_code" => {"$exists"...

How to return distinct $or in mongodb?

mongodb,mongodb-query,aggregation-framework
So I have this query db.collection.find($or:[{data_id:123},{data_id:345},{data_id:443}]); How do I tweak it to return only one of each part of the $or. I.E something analogous to the SQL: SELECT DISTINCT data_id, [...] WHERE data_id='123' OR data_id='345'... ...

Find and groupby

mongodb,mongodb-query,aggregation-framework
If I have the following documents in my collection { "_id" : 1, "domainName" : "test1.com", "hosting1" : "hostgator.com", "hosting2" : "hostgator.com", sID : 1} { "_id" : 2, "domainName" : "test2.com", "hosting1" : "cloud.google.com", "hosting2" : "aws.amazon.com", sID : 2} { "_id" : 3, "domainName" : "test3.com", "hosting1" :...

MongoDB: aggregation - group condition if not empty

mongodb,aggregation-framework
If have data like this: { user: "max", uploaded: <timestamp>, updated: <timestamp> } { user: "tim", uploaded: <timestamp> } Ever user can have different Items. Now I use the mongoDB aggregation framework with $match + $group like this: aggregate([ { $match: {"username": "foouser"}}, { $group: { ...other items.. "last_active": max(updated)...

find documents having a specific count of matches array

arrays,mongodb,aggregation-framework
I've searched high and low but not been able to find what i'm looking for so apologies if this has already been asked. Consider the following documents { _id: 1, items: [ { category: "A" }, { category: "A" }, { category: "B" }, { category: "C" }] }, {...

MongoDB Aggregation: creating subdocuments

mongodb,aggregation-framework
I have a MongoDB collection containing documents like these: { "_id": ObjectId("..."), "code": 22726, "parent_code": 35481, "parent_description": "posterdati", }, { "_id": ObjectId("..."), "code": 22726, "parent_code": 35484, "parent_description": "vicesindaco", }, { "_id": ObjectId("..."), "code": 22727, "parent_code": 35487, "parent_description": "prefettura", } I want to $group them using the "code" field as _id,...

Aggregate mongodb records with self-defined time period

javascript,mongodb,mongodb-query,aggregation-framework
I have a Collection that contains some event reported by client, such as: { "_id" : ObjectId("54f43159c922ac0b4387ef9c"), "appversion" : "v1.2", "appid" : "930370913", "clkip" : "", "actip" : "", "clktime" : 1425289561, "acttime" : 0, "platform" : "google", "isnotified" : false, "idfa" : "14A900D9-A61A-41DC-A327-96EBE4BA57B31" } { "_id" : ObjectId("54f43159c922ac0b4387ef9d"), "appversion"...

$avg in mongodb aggregation

mongodb,aggregation-framework
Document looks like this: { "_id" : ObjectId("361de42f1938e89b179dda42"), "user_id" : "u1", "evaluator_id" : "e1", "candidate_id" : ObjectId("54f65356294160421ead3ca1"), "OVERALL_SCORE" : 150, "SCORES" : [ { "NAME" : "asd", "OBTAINED_SCORE" : 30}, { "NAME" : "acd", "OBTAINED_SCORE" : 36} ] } Aggregation function: db.coll.aggregate([ {$unwind:"$SCORES"}, {$group : { _id : { user_id...

MongoDB mapReduce method unexpected results

mongodb,mapreduce,mongodb-query,aggregation-framework
I have 100 documents in my mongoDB, assuming each of them are possible duplicate with other document(s) in different conditions, such as firstName & lastName, email and mobile phone. I am trying to mapReduce these 100 documents to have the key-value pairs, like grouping. Everything works fine until I have...

Query Mongodb for Sub - Subdocument

arrays,json,mongodb,nosql,aggregation-framework
I have a mongodb document with the following format { inner : [{ name : "I don't this", anotherName: "I don't want this", wayInside : [{ something : 'skip this', somethingElse : 'skip this', someArray: { stuff:'I want this', morestuff: { field3: "Show me", field2: "Show me", field3: [{ "show...

Mongo : Group and push more than 1 element

javascript,mongodb,aggregation-framework
I have a mongodb collection data as per below;I want to group Speciality,Code and Age fields based on EmployeedID( i.e 0001). { "_id" : ObjectId("54d0512191a4da7736e9db43"), "EmployeeID" : "0001", "Speciality" : "xxx", "Code" : "P", "Age" : 8 } /* 1 */ { "_id" : ObjectId("54d0512191a4da7736e9db44"), "EmployeeID" : "0002", "Speciality" :...

Promote subdocument properties to primary document

mongodb,aggregation-framework
I have a $match and $project in an aggregate pipeline that return documents similar to this { _id: "KNYBROOK136", lastOb : { temp : 56 tzname : "America/New_York" } } { _id: "KNYBROOK45", lastOb : { temp : 53 tzname : "America/New_York" } } What can I add to the...

Group array after unwind and match

mongodb,mongoose,aggregation-framework
I have a twice nested schema: mongoose.model('Team', mongoose.Schema( { players : [{ trikots : [{ isNew : Boolean, color : String }] }] }) For now, my query looks like this: Team.aggregate() .match({'_id' : new ObjectId(teamId)}) .unwind('players') .unwind('players.trikots') .match({'players.trikots.isNew' : 'red', 'players.trikots.isNew' : true}) .exec(sendBack); But I would like to...

Combine multiple groups in an aggregation in mongodb

mongodb,aggregation-framework
If I have a collection like this: { "store" : "XYZ", "total" : 100 } { "store" : "XYZ", "total" : 200 } { "store" : "ABC", "total" : 300 } { "store" : "ABC", "total" : 400 } I can get the $sum of orders in the collection by...

When to use map reduce over Aggregation Pipeline in MongoDB?

mongodb,mapreduce,aggregation-framework
While looking at documentation for map-reduce, I found that: NOTE: For most aggregation operations, the Aggregation Pipeline provides better performance and more coherent interface. However, map-reduce operations provide some flexibility that is not presently available in the aggregation pipeline. I did not understand much from it. What are the use...

Mongo: Group, push and sort

node.js,mongodb,aggregation-framework
I have a mongodb collection data as per below;I want to group by EmployeedID( i.e 0001) and then sort(by age) { "_id" : ObjectId("54d0512191a4da7736e9db43"), "EmployeeID" : "0001", "Speciality" : "xxx", "Code" : "P", "Age" : 8 } /* 1 */ { "_id" : ObjectId("54d0512191a4da7736e9db44"), "EmployeeID" : "0002", "Speciality" : "yyyyy",...

MongoDB date range query throws syntax errors when used in aggregation

java,mongodb,mongodb-query,aggregation-framework
I am using Java Driver for MongoDB to query database. I am getting syntax errors when trying to execute range query on a date field in aggregation. It works fine if I use it as part of find query only. Aggregation is the problem here. I used the following Java...

mongoDB: Projection of nested arrays

mongodb,mongodb-query,aggregation-framework
I've got a collection "accounts" which contains documents similar to this structure: { "email" : "[email protected]", "groups" : [ { "name" : "group1", "contacts" : [ { "localId" : "c1", "address" : "some address 1" }, { "localId" : "c2", "address" : "some address 2" }, { "localId" : "c3",...

Aggregating heterogeneous documents in MongoDB

mongodb,mapreduce,aggregation-framework
I am writing a tool to inspect a MongoDB session store, in which the structure is namespaces containing variables containing information on the variables, roughly: { "some namespace" : { "some variable" : { "t" : "serialization type", "b" : "data value", "c" : "java class type" }, "some other...

Search Exact Array Values In Multiple Fields

arrays,mongodb,mongodb-query,aggregation-framework
I have a collection which has 3 documents like below: Collection: { name: "A", arr: [1, 2, 3], arr1: [4, 5, 6] }, { name: "B", arr: [3, 7, 11], arr1: [5, 6, 9] }, { name: "C", arr: [3, 4, 5], arr1: [7, 9, 12] } I want to...

MongoDB {aggregation $match} vs {find} speed

mongodb,aggregation-framework
I have a mongoDB collection with millions of rows and I'm trying to optimize my queries. I'm currently using the aggregation framework to retrieve data and group them as I want. My typical aggregation query is something like : $match > $group > $ group > $project However, I noticed...

Using weights for searching in mongoose

node.js,mongodb,mongoose,mongodb-query,aggregation-framework
So I've read through this but still am a little bit confused on how to go about this. My model contains various fields that are Strings, Numbers and Boolean values. $text seems like it can only take in strings. What if I wanted to do a search like: model.find({petsAllowed:true, rooms:4,...

How to populate a referenced document from other collection and query?

node.js,mongodb,mongoose,aggregation-framework,aggregation
Here is the sample collections for the query, I want the result like given below. How can I show the details of device and user's details in issuedDevices collection's document using mongo/nodejs query, please help: 1. collection user /* 0 */ { "_id" : 1, "name" : "Pradeep", "email" :...

Return MongoDB results grouped by geometry

mongodb,mapreduce,aggregation-framework,geojson
Is it possible to query a collection in MongoDB and return the results grouped by geometric boundaries? For instance, I have one collection of addresses, each of which has a lat/long defined as a GeoJSON Point. I also have a collection of city council wards, each of which has a...

List values in MongoDB aggregation pipeline belonging to different documents

mongodb,aggregation-framework
I got this result coming from an Aggregation pipeline I did: { "_id" : ISODate("2015-05-14T08:22:09.441Z"), "values" : { "v1_min" : 15.872267760931187, "v1" : 15.909139078185774, "v1_max" : 20.6420184124931776 } }, { "_id" : ISODate("2015-05-13T08:22:09.441Z"), "values" : { "v1_min" : 2.872263320931187, "v1" : 7.909132898185774, "v1_max" :44.6498764124931776 } }, {...} do you think...

Efficient way to aggregate in Mongodb

python,mongodb,pymongo,aggregation-framework
I have a collection { "name" : "foo" "clicked" : {"0":6723,"1": 1415,"2":1122} } { "name" : "bar" "clicked" : {"8":1423,"9": 1415,"10":1122} } { "name" : "xyz" "clicked" : {"22":6723,"23": 1415,"2":1234} } clicked is basically {"position of item-clicked in the list" : "id of the item"} The final output which I...