Questions tagged [mongodb-aggregation]

MongoDB is a NoSQL database with the intent of storing billions of records. It offers the aggregation framework as a tool to manipulate and query records. This tag is intended to be used with question specific to this aggregation framework, since aggregation is too general.

147 questions
3
votes
1 answer

sum number of occurrences on two fields in mongodb

I have a collection of documents called games who looks like this { winner: 'oakis', loser: 'test' }, { winner: 'test', loser: 'oakis' } Now I would like MongoDB to output this: { _id: 'oakis', wins: 1, losses: 1 }, { _id: 'test', wins: 1, losses:…
Anders Ekman
  • 323
  • 5
  • 17
3
votes
1 answer

MongoDB - filtering an array based on another array

I'm using MongoDB and this is what my documents look like { "_id": 1, "arr1": ["a", "a", "b", "c"], "arr2": [1, 4, 2, 3 ], }, { "_id": 2, "arr1": ["z", "a", "b", "a"], "arr2": [1, 4, 2, 3 ], } The arrays in keys arr1 and arr2…
ucaiado
  • 623
  • 1
  • 5
  • 10
2
votes
0 answers

Operation inside mongo db projection in aggregate query

I am working on a search feature. I have an object containing data in key value pair where key is an id of an object and value is a score that I have counted with some background process. So my case during search fora data I have to add the score…
2
votes
1 answer

MongoDb aggregation query with $group and $push into subdocument

I have a question regarding the $group argument of MongoDb aggregations. My data structure looks as follows: My "Event" collection contains this single document: { "_id": ObjectId("mongodbobjectid..."), "name": "Some Event", …
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187
2
votes
1 answer

Aggregate query to fetch data which does not have blank("") or null value is not working properly

I have this aggregate query to fetch data which does not have blank("") value or null value in my collection. Here is my query : MyCollectionA.aggregate([ {$lookup: { "from" : "MyCollectionB","localField" : "MyCollectionAId", "foreignField" :…
2
votes
2 answers

Mongoose count certain element in an embedded documents array

I am using mongoose 4.6.3. I have the following schema : var mongoose = require('mongoose'); var User = require('./User'); var TicketSchema = new mongoose.Schema({ user : { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true…
AlexB
  • 3,518
  • 4
  • 29
  • 46
2
votes
1 answer

How to filter data between two times from hh:mm to hh:mm in mongoDB

Mongoose var filter = {}; filter.strBillDate = { "$gte": new Date(req.params.fromdate), "$lt": new Date(req.params.todate) }; return Sales .aggregate([{ $match: filter }, { "$project": { …
Naveen
  • 757
  • 3
  • 17
  • 41
2
votes
1 answer

Node with mongoose aggregate and sum nested fields

When I do this query against my collection... models.Project.find(function(err, result) { //result = doc below }).populate('media') ... I get this result: { _id: 57f36baa6cf34d079c8474a0, code: 'ZMIA', __v: 0, media:[ { _id:…
2
votes
2 answers

How to get aggregation of child collection in mongodb

I have a data structure like this: { "_id": "order1", "transactions": [ { "amount": 100, "type": "payment" }, { "amount": -10, "type": "refund" } ] } I want to get sum of amount which is 100+(-10) = 90.…
2
votes
2 answers

Use $group with find in mongodb node.js

I want to group by user_id from order collection where there are lots of entries but I want to fetch the top 5 users that have made more orders and sum the overall price of particular user. For example, if I made 10 orders I want to fetch my id and…
2
votes
1 answer

merge two fields into an object

I am trying to merge two fields here is my collection db.Acc_Details.findOne() { "_id": ObjectId("577f43fe748646cc91370713"), "Acc_Id": 1, "Name": "xxxxx", "Phone": NumberLong("123456789"), "Email": "xxxxxx@gmail.com"…
2
votes
1 answer

Mongodb use multiple group operator in single aggregation

I am using mongodb aggregation for getting counts of different fields. Here are some documents from the mobile collection:- { "title": "Moto G", "manufacturer": "Motorola", "releasing": ISODate("2011-03-00T10:26:48.424Z"), "rating":…
vineet
  • 13,832
  • 10
  • 56
  • 76
2
votes
1 answer

mongodb aggregate filter and count on nodejs

I have this collection named "points" [ { 'guid': 'aaa' }, { 'guid': 'bbb' }, { 'guid': 'ccc' }, ] and I have another named "stream" [ { 'title': 'pippo', 'guid': 'aaa', 'email': 'pippo@disney'}, { 'title': 'pluto', 'guid':…
2
votes
1 answer

Group books and get count for each of their ratings

I have a Rating model with a book and rating value to it. I would like to get all the ratings count (ratings vary from 1 to 5) for each book in the database. My schema simply looks like - { "_id": ObjectId("57e112312a52fe257e5d1d5c"), …
2
votes
1 answer

How to use Mongo aggregate feature to compute Totals and SubTotal Percentages with Nested Groups?

I have some documents in mongo collection like this. { "_id" : ObjectId("57e290087139be15d59408c1"), "groupName" : "Registration", "testCases" : [ { "name" : "R1", "browser" : "Chrome", …
1 2
3
9 10