Questions tagged [aggregation-framework]
The MongoDB Aggregation Framework provides a means to reshape and aggregate data in MongoDB 2.2+.
12,332
questions
1
vote
1
answer
31
views
How to add a specific field into array of objects in mongodb projection
I've a query like below, It joins 3 tables and projects data, the query looks like this.
db.incomedistros.aggregate([
{
$lookup: {
from: "orders",
localField:...
0
votes
0
answers
21
views
Dividing an element from one array by an element in a different array in mongodb aggregation
Here's my current code:
const mostPlayedCards = async (cardType, colorIdentity) => {
const coloridentity = await decksByGlobalColorIdentity();
const totalDecks = await Deck.aggregate([
...
1
vote
1
answer
37
views
Mongo $lookup not working after another $group
I have an aggregation query like this to group the categories which have a post
const categoriesWithPosts = await Models.Post.aggregate([
{
$match: { active: true },
},
{
...
0
votes
1
answer
34
views
Union with two collections is not outputting expected result
I am usign mongo version as v5.0.19
I have 2 collections with the data in it as below:
Collection 1 - external_S_P_FLAT_main_api:
[
{
"_id": {
"$oid": "...
1
vote
1
answer
42
views
How to fetch related user info when fetching all posts in MongoDB
Hello I am new to MongoDB. I want to fetch all posts with user details of who created the post. I have two collections.
users collection:
[{
"_id": {
"$oid": "...
0
votes
2
answers
62
views
Is it possible to join subqueries using MongoDB aggregation?
Is there a MongoDB aggregation query equivalent to the following SQL statement using a single MongoDB aggregation? (The two operands of the JOIN are two subqueries that also include join operations.)
...
0
votes
0
answers
29
views
MongoDB grouping on Subdocument Query Performance on Large Dataset
I have a large collection in mongodb. It has around a million documents in that collection. But when i run aggregation pipeline on it, it takes almost 60 seconds to return unique values. I cant get ...
0
votes
0
answers
26
views
After some aggregation stages, I want the unwind result with other collections fields with their values as blank/empty
I am usign mongo version as v5.0.19
I have 2 collections with the data in it as below:
Collection 1 - external_S_P_FLAT_main_api:
[
{
"_id": {
"$oid": "...
0
votes
1
answer
32
views
MongoDB query to transform documents
I have the documents structured in MongoDB 6.0 as below:
[
{
"_id" : ObjectId("6555a7d7c2d9de74abb09de8"),
"tagName" : "tag1",
"...
0
votes
0
answers
19
views
Issue with Projection in MongoDB C# Driver Using $facet Stage
Background:
I'm working with the MongoDB C# driver and facing a projection issue when using the $facet stage in an aggregation pipeline. The projection works as expected in a simple scenario but fails ...
-2
votes
0
answers
49
views
how to get the current index in a $map?
https://www.mongodb.com/docs/manual/reference/operator/aggregation/map/
Is it possible to extract the index of the current element in $map?
0
votes
2
answers
85
views
MongoDB Aggregation on nested levels
I use MongoDB 4.4.
I'm trying to make a Mongo aggregation query that would return me a document with nested arrays. To specify the context:
I have offers, which contain modules, which contain items, ...
0
votes
0
answers
25
views
How do I chain Group expressions in MongoDb for C#?
I'm new to MongoDb and I'm trying to return a count of unique values of certain properties from a query using the C# driver.
I can do this for each field individually as follows:
var ...
0
votes
0
answers
51
views
MongoDB aggregation with $match $skip $limit and order guarantee
Does the following aggregation guarantee a default ordering for the documents assuming that the $match returns unique documents? Or we always have to include a sort step to have consistent results ...
0
votes
0
answers
15
views
In mongodb c# how can I limit a facet search to a specific set of ids?
Here is what I would like to accomplish:
var idFilter = Builders<TProduct>.Filter.In(x => x.Id, aListOfStringIds);
var aggResultArray = await Repository.Aggregate()
.SearchMeta(Builders&...
1
vote
1
answer
137
views
How to find ids for products that are not on sale but exist in the product details collection using aggregation pipeline
I am joining a products collection on a productDetail collection where the product _id is the _id in the productDetail collection. I want to be able to find the products which are not on sale but ...
0
votes
1
answer
36
views
mongodb atlas search embedded document and project only the matched array elements
I have a mongo db collection structured something like below.
{
_id,
name,
companies: [{
name,
rating
address
}]
}
I have created a atlas index of the type embedded on the field companies.name, when ...
1
vote
1
answer
36
views
MongoDB group aggregate with nested array of objects
I have a collection with the following structure (i'll post a link to playground because it's quite big):
https://mongoplayground.net/p/WNga0mze0WS
Just a brief explanation, we are looking for ...
0
votes
0
answers
18
views
How to apply $lookup on array elements without $unwind
I have an faqs array inside my provider schema, containing a title and content as a reference to a translations collection. How do i get an object containing all matching docs in place of title and ...
0
votes
1
answer
37
views
Django ORM aggregation with conditional SUM with group by failing
I'm trying to get the follwing query to execute in Django ORM but no luck so far.
I would like to have all PotatoesMeasurement grouped by wn and do a conditional count on alternaria when it greater ...
0
votes
1
answer
35
views
MongoDB group by field and count
I have the following collection data
{
"_id": 1,
"date": "2023-11-08",
"field": "A"
},
{
"_id": 2,
"date": "2023-11-...
2
votes
1
answer
51
views
Select nested fields in filtered aggregation
How can I aggregate, filter nested array and project selected nested fields?
I have the following data:
{
"teacher": "Ben",
"students": [
{
"name":...
0
votes
0
answers
29
views
Aggregation vs Multiple fetch and update how it performs?
I have a aggregation pipeline where the match criteria is generated dynamically by end user and the updates as well. Right now I convert the user input to a aggregation pipeline and I have some ...
1
vote
1
answer
39
views
Combine two mongoose aggregation pipeline into one where result of first pipeline is used in another pipeline
I have a collection named dummy .
It's schema is defined as -
const schema = mongoose.Schema(
{
Name: { type: String },
start: { type: String },
end: { type: String },
}
Now I want to ...
0
votes
1
answer
38
views
MongoDB Aggregation - Find the products with low stock
I'm trying to create a MongoDB aggregation query to find products with low stock. Each product in my collection has a low_stock quantity specified, and I want to retrieve all product stocks whose ...
-1
votes
1
answer
52
views
Struggling with MongoDB grouping by year, month and day query [duplicate]
DB STRUCTURE
date: 2022-01-01T03:28:00.000+00:00 (Date)
value: XXX (int)
write a queries to group documents by year, month and day summing relative values
filter_cond = {"$match": {"dt&...
2
votes
1
answer
35
views
MongoDB: How do I get multiple $indexOfArray values?
I have a document where the index of an array holds meaning. In a MongoDB aggregation, How do I return ALL of the indices in an array that matches a specific value? $indexOfArray only returns the ...
2
votes
1
answer
42
views
Mongoose lookup from 2 collections gives wrong sum values
I Have the following collections
Entity :
const Entity = new Schema(
{
name: {
type: String,
required: true,
},
type: {
type: String,
required: true,
enum: [...
0
votes
0
answers
50
views
How to get below format output data from Mongodb collection
expected output example: {count of job:number,sum of assets:number,count of job group by status:number,list of policy:policyname +policyid}.
{
"_id" : ObjectId("...
0
votes
1
answer
47
views
Any functionality in a MongoDB Aggregation like a simple "regex replace"?
I'm trying to create a step in a MongoDB aggregation pipeline which will replace the value of a full Social Security Number (like "123-45-6789") with a masked value, preserving the last four ...
0
votes
0
answers
15
views
MongoTemplate Aggregation for nested objects and multiple unwinds
I am using for a Mongotemplate with spring boot to implement a whatsapp like stories implementation. My story model is as follows:
private String id;
private String userId;
private String ...
0
votes
1
answer
46
views
updateOne query only modifying first matching element of array despite multiple matches
https://mongoplayground.net/p/zIhLOT8YXLl
document schema:
{
_id: ObjectId();
array_property: [ {_id: ObjectId(), img_array: ["string", "string"] }, { }, ... ]
}
query:
await ...
0
votes
0
answers
41
views
How to get a collection in MongoDB sorted by days per month
Is it possible to create an aggregation where we get an array of days in a month and fill it with relevant data?
For example, we have expenses. I want to get all the expenses in November.
{
title: &...
0
votes
0
answers
53
views
MongoDB graphLookup results different from manual recursive find() queries
For others that stumble upon it. I created a much smaller example of this that can be observed @ https://mongoplayground.net/p/lXxoTdlY-fa
But now I'm thinking it's my understanding of $graphLookup ...
1
vote
1
answer
15
views
Mongo Db nested grouping with applying start and end time filter
hi i want create nested group in aggregation, first group should be with category id and next group should be data between a day, starting from input start time from api, i need to group categories, ...
0
votes
1
answer
40
views
MongoDB - match against a field, and get the first records which have distinct values in another field?
I'm looking for a very performant MongoDB query that will match based on a field, 'matchField', and only return the first document which has a distinct different field, 'batchId'.
Given a collection ...
0
votes
0
answers
72
views
MongoDB Cosine Similarity Search with Source Filtering
I have a database in Cosmos containing around 1500 JSON documents. Some of these documents have the field 'source' set to 'A', while others have 'source' set to 'B'. To find the four most similar ...
0
votes
0
answers
16
views
Mongoose does mot properly handle date string in aggregation unless I hardcode it
I have a MongoDb orders collection that has a field called order_date which indicates the year a customer made a purchase. I want to be able to query for a customer's orders up to 5 years ago. The ...
0
votes
1
answer
22
views
Group By After LookUp
I have to count how many customers we have "Active" and "Inactive" in our system.
I could just group by Status and have it.
The problem is that each customer belongs to an office ...
0
votes
2
answers
34
views
How to bring values from array of embedded documents into the root on MongoDB?
I have the following record:
id: 119,
customfields: Array (2)
0: Object
value: "Mr"
type: "salutation"
1: Object
value: "Google"
type: "company&...
1
vote
1
answer
44
views
MongoDB Pipeline $lookup only works when hard coding object id
I'm experiencing a weird issue in one of my MongoDB aggregation pipeline stages.
Here are the results from the previous stage:
[
{
"_id": {
"$oid": "...
0
votes
1
answer
62
views
How to group by two fields whose value my be same and count them
i have a collection named players, below is the sample document. now i want to fetch the user-wise count of players for which the user is either mentor or owner or both. help me with a mongodb ...
0
votes
1
answer
33
views
Parse out numbers from multiple String fields, add them to a new field in MongoDB aggregation pipeline?
I am trying to come up with a way to parse out the "numbers" in three different fields of type string, convert them to true number types, and add them together into a new field. I have to do ...
0
votes
1
answer
17
views
Create an aggregate list of values and number of occurrences in MongoDB
Given the following documents:
[
{
_id: "N0001",
name: "Bruce Lee",
skills: ["karate", "jiu jitsu"],
},
{
_id: "N0002",
name:...
0
votes
0
answers
23
views
Elasticsearch using to filter detailed data out of a bif demand list
I have a huge CSV file with three columns:
number of product, KindOfProduct, FinalSubmissionTime
The data is for a whole month and the FinalSubmissionTime is specified with the date and hour. Now I ...
0
votes
1
answer
36
views
MongoDB $lookup on doubly nested array of objects
I have a data model where each product has many variants and each variant has many modifications. In the database, it looks like this:
const mods = db.modifications.insertMany([
{
title: '...
0
votes
1
answer
17
views
Aggregating mongo documents where one specific value is one group and rest are in the other group
I have below structure of data in my mongo collection:
{app: 'app1', status: 'success', count: 5},
{app: 'app1', status: 'fail', count: 4},
{app: 'app1', status: 'blocked', count: 3},
{app: 'app1', ...
0
votes
0
answers
27
views
_id match not working in lookup mongodb aggregate
I’ve tried converting to string to objectId but its still not working, this is the lookup thats not working:
{
$lookup: {
from: "comments",
let: { refId: "$_id&...
0
votes
0
answers
49
views
Getting duplicate records when using skip, limit, and sort with MongoDB aggregation
I have the following query which I am using to paginate the response with an increment of 15. While iterating through the paginated results, I am seeing some duplicate orderNumbers that was already ...
0
votes
1
answer
32
views
why i didn't get The same result for these two different queries for Get actors who have appeared in at least 5 films
I'd like to retrieve a list of actors who have appeared in a minimum of 5 films using two different queries.
In the first query, $unwind is not used, but within the $group stage, it matches every ...