Questions tagged [aggregation-framework]
The MongoDB Aggregation Framework provides a means to reshape and aggregate data in MongoDB 2.2+.
12,567
questions
544
votes
12
answers
392k
views
Update MongoDB field using value of another field
In MongoDB, is it possible to update the value of a field using the value from another field? The equivalent SQL would be something like:
UPDATE Person SET Name = FirstName + ' ' + LastName
And the ...
485
votes
20
answers
489k
views
Retrieve only the queried element in an object array in MongoDB collection
Suppose you have the following documents in my collection:
{
"_id":ObjectId("562e7c594c12942f08fe4192"),
"shapes":[
{
"shape":"square",
"color":"blue"
},
...
348
votes
9
answers
510k
views
MongoDB SELECT COUNT GROUP BY
I am playing around with MongoDB trying to figure out how to do a simple
SELECT province, COUNT(*) FROM contest GROUP BY province
But I can't seem to figure it out using the aggregate function. I ...
315
votes
11
answers
481k
views
MongoDB: Combine data from multiple collections into one..how?
How can I (in MongoDB) combine data from multiple collections into one collection?
Can I use map-reduce and if so then how?
I would greatly appreciate some example as I am a novice.
234
votes
10
answers
270k
views
Find duplicate records in MongoDB
How would I find duplicate fields in a mongo collection.
I'd like to check if any of the "name" fields are duplicates.
{
"name" : "ksqn291",
"__v" : 0,
"_id" : ObjectId("...
193
votes
4
answers
352k
views
mongodb group values by multiple fields
For example, I have these documents:
{
"addr": "address1",
"book": "book1"
},
{
"addr": "address2",
"book": "book1"
},
{
"addr": "address1",
"book": "book5"
},
{
"addr": "address3",
"...
180
votes
6
answers
130k
views
Include all existing fields and add new fields to document
I would like to define a $project aggregation stage where I can instruct it to add a new field and include all existing fields, without having to list all the existing fields.
My document looks like ...
172
votes
10
answers
285k
views
mongodb count num of distinct values per field/key
Is there a query for calculating how many distinct values a field contains in DB.
f.e I have a field for country and there are 8 types of country values (spain, england, france, etc...)
If someone ...
152
votes
3
answers
89k
views
Mongodb Explain for Aggregation framework
Is there an explain function for the Aggregation framework in MongoDB? I can't see it in the documentation.
If not is there some other way to check, how a query performs within the aggregation ...
151
votes
8
answers
210k
views
$lookup on ObjectId's in an array
What's the syntax for doing a $lookup on a field that is an array of ObjectIds rather than just a single ObjectId?
Example Order Document:
{
_id: ObjectId("..."),
products: [
ObjectId("..<...
145
votes
5
answers
129k
views
What's the $unwind operator in MongoDB?
This is my first day with MongoDB so please go easy with me :)
I can't understand the $unwind operator, maybe because English is not my native language.
db.article.aggregate(
{ $project : {
...
137
votes
3
answers
105k
views
MongoDB aggregation framework match OR
Is it possible to do an OR in the $match?
I mean something like this:
db.articles.aggregate(
{ $or: [ $match : { author : "dave" }, $match : { author : "john" }] }
);
137
votes
3
answers
253k
views
How to filter array in subdocument with MongoDB [duplicate]
I have array in subdocument like this
{
"_id" : ObjectId("512e28984815cbfcb21646a7"),
"list" : [
{
"a" : 1
},
{
"a" : 2
},
{
...
123
votes
11
answers
37k
views
Does MongoDB's $in clause guarantee order
When using MongoDB's $in clause, does the order of the returned documents always correspond to the order of the array argument?
107
votes
3
answers
111k
views
Return only matched sub-document elements within a nested array
The main collection is retailer, which contains an array for stores. Each store contains an array of offers (you can buy in this store). This offers array has an array of sizes. (See example below)
...
101
votes
10
answers
140k
views
MongoDB aggregate by field exists
I have a hard time believing this question hasn't been asked and answered somewhere already, but I can't find any trace of it.
I have a MongoDB aggregation query that needs to group by a boolean: the ...
101
votes
11
answers
121k
views
MongoDB Full and Partial Text Search
Env:
MongoDB (3.2.0) with Mongoose
Collection:
users
Text Index creation:
BasicDBObject keys = new BasicDBObject();
keys.put("name","text");
BasicDBObject options = new ...
95
votes
3
answers
77k
views
Project first item in an array to new field (MongoDB aggregation)
I am using Mongoose aggregation (MongoDB version 3.2).
I have a field users which is an array. I want to $project first item in this array to a new field user.
I tried
{ $project: {
user: '$...
94
votes
23
answers
153k
views
Convert ObjectID (Mongodb) to String in JavaScript
I want to convert ObjectID (Mongodb) to String in JavaScript.
When I get a Object form MongoDB. it like as a object has: timestamp, second, inc, machine.
I can't convert to string.
94
votes
6
answers
64k
views
How to use MongoDBs aggregate `$lookup` as `findOne()`
So as you all know, find() returns an array of results, with findOne() returning just a simply object.
With Angular, this makes a huge difference. Instead of going {{myresult[0].name}}, I can simply ...
93
votes
3
answers
61k
views
MongoDB {aggregation $match} vs {find} speed
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 ...
90
votes
1
answer
137k
views
How to aggregate sum in MongoDB to get a total count?
For some collection with a field { wins: Number }, how could I use MongoDB Aggregation Framework to get the total number of wins across all documents in a collection?
Example:
If I have 3 documents ...
84
votes
18
answers
90k
views
Get a count of total documents with MongoDB when using limit
I am interested in optimizing a "pagination" solution I'm working on with MongoDB. My problem is straight forward. I usually limit the number of documents returned using the limit() functionality. ...
83
votes
3
answers
135k
views
How to get all the values that contains part of a string using mongoose find?
I have the following problem retrieving data from MongoDB using mongoose.
Here is my Schema:
const BookSchema = new Schema(
{
_id:Number,
title:String,
authors:[String],
...
81
votes
2
answers
86k
views
MongoDB nested lookup with 3 levels
I need to retrieve the entire single object hierarchy from the database as a JSON. Actually, the proposal about any other solution to achieve this result would be highly appreciated. I decided to use ...
79
votes
3
answers
39k
views
MongoDB - The argument to $size must be an Array, but was of type: EOO / missing
Trying to create a MongoDB data source with icCube. The idea is to return the size of an array as a new field. Something like :
$project:
{
"people": 1,
"Count myFieldArray" : {$size : "$...
72
votes
4
answers
84k
views
Mongodb Join on _id field from String to ObjectId
I have two collections
User
{
"_id" : ObjectId("584aac38686860d502929b8b"),
"name" : "John"
}
Role
{
"_id" : ObjectId("584aaca6686860d502929b8d"),
"role" : "Admin",
"userId" : "...
71
votes
5
answers
99k
views
MongoDB aggregation with $lookup only include (or project) some fields to return from query
In mongo, after doing an aggregation with $lookup, I would like the request to return only some fields and not the whole document.
I have the following query :
db.somecollection.aggregate([{
$...
70
votes
3
answers
88k
views
Conditional $sum in MongoDB
My collection in mongodb is similar to the following table in SQL:
Sentiments(Company,Sentiment)
Now, I need to execute a query like this:
SELECT
Company,
SUM(CASE WHEN Sentiment >0 THEN ...
68
votes
3
answers
89k
views
MongoDB aggregate within daily grouping [duplicate]
I have some docs in mongo that looks something like this:
{
_id : ObjectId("..."),
"make" : "Nissan",
..
},
{
_id : ObjectId("..."),
"make" : "Nissan",
"saleDate" : ISODate("2013-04-...
68
votes
4
answers
82k
views
How do I rename fields when performing search/projection in MongoDB?
Is it possible to rename the name of fields returned in a find query? I would like to use something like $rename, however I wouldn't like to change the documents I'm accessing. I want just to retrieve ...
67
votes
7
answers
48k
views
Is it possible to rename _id field after mongo's group aggregation?
I have a query like this (simplified):
db.collection.aggregate([
{ $match: { main_id: ObjectId("58f0f67f50c6af16709fd2c7") } },
{
$group: {
_id: "$name",
...
67
votes
4
answers
89k
views
$skip and $limit in aggregation framework
When I read the document I found the following notes:
When a $sort immediately precedes a $limit in the pipeline, the $sort operation only maintains the top n results as it progresses, where n is ...
64
votes
7
answers
43k
views
Group result by 15 minutes time interval in MongoDb
I have a "status" collection like this strcture -
{
_id: ObjectId("545a0b63b03dbcd1238b4567"),
status: 1004,
comment: "Rem dolor ipsam placeat omnis non. Aspernatur nobis qui nisi ...
62
votes
5
answers
60k
views
Find all duplicate documents in a MongoDB collection by a key field
Suppose I have a collection with some set of documents. something like this.
{ "_id" : ObjectId("4f127fa55e7242718200002d"), "id":1, "name" : "foo"}
{ "_id" : ObjectId("4f127fa55e7242718200002d"), "...
60
votes
1
answer
95k
views
MongoDB group by array inner-elements
I've got a list of articles, and each of them has an array property which lists various individuals mentioned in them:
_id: {
$oid: "52b632a9e4f2ba13c82ccd23"
},
providerName: "The Guardian",
url:...
57
votes
6
answers
57k
views
Moongoose aggregate $match does not match id's
I want to show products by ids (56e641d4864e5b780bb992c6 and 56e65504a323ee0812e511f2) and show price after subtracted by discount if available.
I can count the final price using aggregate, but this ...
57
votes
2
answers
20k
views
Is Mongodb Aggregation framework faster than map/reduce?
Is the aggregation framework introduced in mongodb 2.2, has any special performance improvements over map/reduce?
If yes, why and how and how much?
(Already I have done a test for myself, and the ...
56
votes
2
answers
107k
views
Mongoose: how to use aggregate and find together
How can I use aggregate and find together in Mongoose?
i.e I have the following schema:
const schema = new Mongoose.Schema({
created: { type: Date, default: Date.now() },
name: { type: String, ...
55
votes
3
answers
38k
views
$unwind empty array
I have a collection of users where each document has following structure:
{
"_id": "<id>",
"login": "xxx",
"solved": [
{
"problem": "<problemID>",
"points": 10
},...
54
votes
8
answers
33k
views
How to flatten a subdocument into root level in MongoDB?
For example, if I have a document like this
{
a: 1,
subdoc: {
b: 2,
c: 3
}
}
How can I convert it into a format like this? (without using project)
{
a: 1,
b: 2,
c: 3
}
51
votes
3
answers
44k
views
Check if every element in array matches condition
I have a collection of documents:
date: Date
users: [
{ user: 1, group: 1 }
{ user: 5, group: 2 }
]
date: Date
users: [
{ user: 1, group: 1 }
{ user: 3, group: 2 }
]
I would like to query ...
51
votes
3
answers
129k
views
The field "$name" must be an accumulator object
I have a query and when I use a $group a error shows "the field "$name must be an accumulator object", if if remove the filed "$name" all works well and i have tried to use only "name" instead of "$...
50
votes
7
answers
88k
views
Search on multiple collections in MongoDB
I know the theory of MongoDB and the fact that is doesn't support joins, and that I should use embeded documents or denormalize as much as possible, but here goes:
I have multiple documents, such as:
...
49
votes
6
answers
155k
views
Converting string to date in mongodb
Is there a way to convert string to date using custom format using mongodb shell
I am trying to convert "21/May/2012:16:35:33 -0400" to date,
Is there a way to pass DateFormatter or something to
...
49
votes
3
answers
63k
views
how to $project ObjectId to string value in mongodb aggregate?
Is there an operator I could use in aggregate function to get a string instead of ObjectId in response?
db.something.aggregate([
{ "$match": { "property": { "$exists": true } } },
{ "$project": {...
48
votes
4
answers
103k
views
Multiple join conditions using the $lookup operator
Have the two following collections:
// collection1:
{
user1: 1,
user2: 2,
percent: 0.56
}
// collection2:
{
user1: 1,
user2: 2,
percent: 0.3
}
I want to join these two collections on ...
48
votes
5
answers
52k
views
how to group in mongoDB and return all fields in result
I am using aggregate method in mongoDB to group but when I use $group it returns the only field which I used to group. I have tried $project but it is not working either. I also tried $first and it ...
47
votes
8
answers
79k
views
MongoDB Aggregation: Counting distinct fields
I am trying to write an aggregation to identify accounts that use multiple payment sources. Typical data would be.
{
account:"abc",
vendor:"amazon",
}
...
{
account:"abc",
vendor:"overstock",
}
...
47
votes
9
answers
71k
views
Mongo error when using aggregation: sort exceeded memory limit
I get the mongo error exceeded memory limit with error code 16819 when I use aggregation sort.
Im using mongo 2.6.
The query is as follows:
db.BASE_TABLE_CREATION_ExecuteHiveScript_26_V0.aggregate([...