Mongo aggregate remove group if condition in sub array2019 Community Moderator ElectionMongoDB group aggregation of multiple elements and sub-documentsMongo average aggregation query with no groupMongo Aggregation select and push last element in arrayMongo aggregate group by one array elementdefine $group as a list in mongo aggregate queryMongo aggregate group by multiple valuesmongodb aggregation $group and then $push a objectOutput from aggregate array of objects to document keysMongoDB aggregate pipeline groupmongodb aggregation with multiple sub groups
ERC721: How to get the owned tokens of an address
Converting a variable frequency to TTL HIGH and LOW levels, based on a fixed (possible non-fixed?) frequency
Describing a chess game in a novel
Is it normal that my co-workers at a fitness company criticize my food choices?
What is "focus distance lower/upper" and how is it different from depth of field?
How do I change two letters closest to a string and one letter immediately after a string using Notepad++?
What is the Japanese sound word for the clinking of money?
Why does a Star of David appear at a rally with Francisco Franco?
Is there a place to find the pricing for things not mentioned in the PHB? (non-magical)
Is "upgrade" the right word to use in this context?
Why do tuner card drivers fail to build after kernel update to 4.4.0-143-generic?
What did “the good wine” (τὸν καλὸν οἶνον) mean in John 2:10?
Most cost effective thermostat setting: consistent temperature vs. lowest temperature possible
Math equation in non italic font
Is it true that good novels will automatically sell themselves on Amazon (and so on) and there is no need for one to waste time promoting?
Are Roman Catholic priests ever addressed as pastor
Problem with FindRoot
How do I hide Chekhov's Gun?
Shortcut for setting origin to vertex
As a new Ubuntu desktop 18.04 LTS user, do I need to use ufw for a firewall or is iptables sufficient?
Are all passive ability checks floors for active ability checks?
I got the following comment from a reputed math journal. What does it mean?
Planetary tidal locking causing asymetrical water distribution
Why no Iridium-level flares from other satellites?
Mongo aggregate remove group if condition in sub array
2019 Community Moderator ElectionMongoDB group aggregation of multiple elements and sub-documentsMongo average aggregation query with no groupMongo Aggregation select and push last element in arrayMongo aggregate group by one array elementdefine $group as a list in mongo aggregate queryMongo aggregate group by multiple valuesmongodb aggregation $group and then $push a objectOutput from aggregate array of objects to document keysMongoDB aggregate pipeline groupmongodb aggregation with multiple sub groups
At a certain point of my pipeline, I have a structure like this :
[
_id: 1,
elems1: [sub : date: xxx, sub: date: yyy],
elems2: [sub: date: zzz, sub: date: qqq]
,
...
]
Which comes from a grouping :
$group:
_id: '$user.id',
elems1:
$push: sub: '$other'
,
elems2:
$push: sub: '$other'
}
Now I want to delete the groups that have an element in elems1 OR elems2 with sub.date less than a certain date, how could I do this ?
mongodb aggregation-framework
add a comment |
At a certain point of my pipeline, I have a structure like this :
[
_id: 1,
elems1: [sub : date: xxx, sub: date: yyy],
elems2: [sub: date: zzz, sub: date: qqq]
,
...
]
Which comes from a grouping :
$group:
_id: '$user.id',
elems1:
$push: sub: '$other'
,
elems2:
$push: sub: '$other'
}
Now I want to delete the groups that have an element in elems1 OR elems2 with sub.date less than a certain date, how could I do this ?
mongodb aggregation-framework
add a comment |
At a certain point of my pipeline, I have a structure like this :
[
_id: 1,
elems1: [sub : date: xxx, sub: date: yyy],
elems2: [sub: date: zzz, sub: date: qqq]
,
...
]
Which comes from a grouping :
$group:
_id: '$user.id',
elems1:
$push: sub: '$other'
,
elems2:
$push: sub: '$other'
}
Now I want to delete the groups that have an element in elems1 OR elems2 with sub.date less than a certain date, how could I do this ?
mongodb aggregation-framework
At a certain point of my pipeline, I have a structure like this :
[
_id: 1,
elems1: [sub : date: xxx, sub: date: yyy],
elems2: [sub: date: zzz, sub: date: qqq]
,
...
]
Which comes from a grouping :
$group:
_id: '$user.id',
elems1:
$push: sub: '$other'
,
elems2:
$push: sub: '$other'
}
Now I want to delete the groups that have an element in elems1 OR elems2 with sub.date less than a certain date, how could I do this ?
mongodb aggregation-framework
mongodb aggregation-framework
edited Mar 8 at 16:13
HRK44
asked Mar 7 at 15:18
HRK44HRK44
473215
473215
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
When you do your $group, you can also track the "minElemDate" and then $match by that in the next pipeline stage.
$group:
_id: "$user.id",
elem1s: ...,
elem2s: ...
minElemDate:
$min:
$cond: [
$or: [condition1, condition2] ,
"$user.date",
null
]
,
$match: $gte: ["$minElemDate", minDate]
I like this idea, can you please elaborate on what to put incondition1andcondition2? That's the part where I'm confused
– HRK44
Mar 8 at 12:55
$eq: ['$user.name', 'L']and$eq: ['$user.name', 'L']-- it seems like those are the elements that you care about
– willis
Mar 8 at 16:10
I will remove this part, maybe it is making it confusing. I care only about the date... I will update my question
– HRK44
Mar 8 at 16:13
tldr of my answer -- when you do the group, also track the min date. don't try and filter it out afterwards because it'll be harder
– willis
Mar 8 at 16:16
The problem is that I don't know what to put in the$minblock, since I want the min of the object date inside the array
– HRK44
Mar 8 at 16:17
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047190%2fmongo-aggregate-remove-group-if-condition-in-sub-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
When you do your $group, you can also track the "minElemDate" and then $match by that in the next pipeline stage.
$group:
_id: "$user.id",
elem1s: ...,
elem2s: ...
minElemDate:
$min:
$cond: [
$or: [condition1, condition2] ,
"$user.date",
null
]
,
$match: $gte: ["$minElemDate", minDate]
I like this idea, can you please elaborate on what to put incondition1andcondition2? That's the part where I'm confused
– HRK44
Mar 8 at 12:55
$eq: ['$user.name', 'L']and$eq: ['$user.name', 'L']-- it seems like those are the elements that you care about
– willis
Mar 8 at 16:10
I will remove this part, maybe it is making it confusing. I care only about the date... I will update my question
– HRK44
Mar 8 at 16:13
tldr of my answer -- when you do the group, also track the min date. don't try and filter it out afterwards because it'll be harder
– willis
Mar 8 at 16:16
The problem is that I don't know what to put in the$minblock, since I want the min of the object date inside the array
– HRK44
Mar 8 at 16:17
|
show 1 more comment
When you do your $group, you can also track the "minElemDate" and then $match by that in the next pipeline stage.
$group:
_id: "$user.id",
elem1s: ...,
elem2s: ...
minElemDate:
$min:
$cond: [
$or: [condition1, condition2] ,
"$user.date",
null
]
,
$match: $gte: ["$minElemDate", minDate]
I like this idea, can you please elaborate on what to put incondition1andcondition2? That's the part where I'm confused
– HRK44
Mar 8 at 12:55
$eq: ['$user.name', 'L']and$eq: ['$user.name', 'L']-- it seems like those are the elements that you care about
– willis
Mar 8 at 16:10
I will remove this part, maybe it is making it confusing. I care only about the date... I will update my question
– HRK44
Mar 8 at 16:13
tldr of my answer -- when you do the group, also track the min date. don't try and filter it out afterwards because it'll be harder
– willis
Mar 8 at 16:16
The problem is that I don't know what to put in the$minblock, since I want the min of the object date inside the array
– HRK44
Mar 8 at 16:17
|
show 1 more comment
When you do your $group, you can also track the "minElemDate" and then $match by that in the next pipeline stage.
$group:
_id: "$user.id",
elem1s: ...,
elem2s: ...
minElemDate:
$min:
$cond: [
$or: [condition1, condition2] ,
"$user.date",
null
]
,
$match: $gte: ["$minElemDate", minDate]
When you do your $group, you can also track the "minElemDate" and then $match by that in the next pipeline stage.
$group:
_id: "$user.id",
elem1s: ...,
elem2s: ...
minElemDate:
$min:
$cond: [
$or: [condition1, condition2] ,
"$user.date",
null
]
,
$match: $gte: ["$minElemDate", minDate]
edited Mar 8 at 16:11
answered Mar 7 at 20:10
williswillis
79429
79429
I like this idea, can you please elaborate on what to put incondition1andcondition2? That's the part where I'm confused
– HRK44
Mar 8 at 12:55
$eq: ['$user.name', 'L']and$eq: ['$user.name', 'L']-- it seems like those are the elements that you care about
– willis
Mar 8 at 16:10
I will remove this part, maybe it is making it confusing. I care only about the date... I will update my question
– HRK44
Mar 8 at 16:13
tldr of my answer -- when you do the group, also track the min date. don't try and filter it out afterwards because it'll be harder
– willis
Mar 8 at 16:16
The problem is that I don't know what to put in the$minblock, since I want the min of the object date inside the array
– HRK44
Mar 8 at 16:17
|
show 1 more comment
I like this idea, can you please elaborate on what to put incondition1andcondition2? That's the part where I'm confused
– HRK44
Mar 8 at 12:55
$eq: ['$user.name', 'L']and$eq: ['$user.name', 'L']-- it seems like those are the elements that you care about
– willis
Mar 8 at 16:10
I will remove this part, maybe it is making it confusing. I care only about the date... I will update my question
– HRK44
Mar 8 at 16:13
tldr of my answer -- when you do the group, also track the min date. don't try and filter it out afterwards because it'll be harder
– willis
Mar 8 at 16:16
The problem is that I don't know what to put in the$minblock, since I want the min of the object date inside the array
– HRK44
Mar 8 at 16:17
I like this idea, can you please elaborate on what to put in
condition1 and condition2 ? That's the part where I'm confused– HRK44
Mar 8 at 12:55
I like this idea, can you please elaborate on what to put in
condition1 and condition2 ? That's the part where I'm confused– HRK44
Mar 8 at 12:55
$eq: ['$user.name', 'L'] and $eq: ['$user.name', 'L'] -- it seems like those are the elements that you care about– willis
Mar 8 at 16:10
$eq: ['$user.name', 'L'] and $eq: ['$user.name', 'L'] -- it seems like those are the elements that you care about– willis
Mar 8 at 16:10
I will remove this part, maybe it is making it confusing. I care only about the date... I will update my question
– HRK44
Mar 8 at 16:13
I will remove this part, maybe it is making it confusing. I care only about the date... I will update my question
– HRK44
Mar 8 at 16:13
tldr of my answer -- when you do the group, also track the min date. don't try and filter it out afterwards because it'll be harder
– willis
Mar 8 at 16:16
tldr of my answer -- when you do the group, also track the min date. don't try and filter it out afterwards because it'll be harder
– willis
Mar 8 at 16:16
The problem is that I don't know what to put in the
$min block, since I want the min of the object date inside the array– HRK44
Mar 8 at 16:17
The problem is that I don't know what to put in the
$min block, since I want the min of the object date inside the array– HRK44
Mar 8 at 16:17
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55047190%2fmongo-aggregate-remove-group-if-condition-in-sub-array%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown