mongodb where not exists (subtract two query results)
consider I have the following collection,
[
"user_id": 7,
"action": 1
,
"user_id": 8,
"action": 1
,
"user_id": 9,
"action": 1
,
"user_id": 7,
"action": 2
]
I need to find all users who has for example action 1 but not action 2.
In mysql It's possible to just select user_id with two queries and subtract this results, or use where not exists subquery.
how can I handle this with mongodb?
so my expected result is :
[8,9]
mongodb mongodb-query aggregation-framework
add a comment |
consider I have the following collection,
[
"user_id": 7,
"action": 1
,
"user_id": 8,
"action": 1
,
"user_id": 9,
"action": 1
,
"user_id": 7,
"action": 2
]
I need to find all users who has for example action 1 but not action 2.
In mysql It's possible to just select user_id with two queries and subtract this results, or use where not exists subquery.
how can I handle this with mongodb?
so my expected result is :
[8,9]
mongodb mongodb-query aggregation-framework
add a comment |
consider I have the following collection,
[
"user_id": 7,
"action": 1
,
"user_id": 8,
"action": 1
,
"user_id": 9,
"action": 1
,
"user_id": 7,
"action": 2
]
I need to find all users who has for example action 1 but not action 2.
In mysql It's possible to just select user_id with two queries and subtract this results, or use where not exists subquery.
how can I handle this with mongodb?
so my expected result is :
[8,9]
mongodb mongodb-query aggregation-framework
consider I have the following collection,
[
"user_id": 7,
"action": 1
,
"user_id": 8,
"action": 1
,
"user_id": 9,
"action": 1
,
"user_id": 7,
"action": 2
]
I need to find all users who has for example action 1 but not action 2.
In mysql It's possible to just select user_id with two queries and subtract this results, or use where not exists subquery.
how can I handle this with mongodb?
so my expected result is :
[8,9]
mongodb mongodb-query aggregation-framework
mongodb mongodb-query aggregation-framework
edited Nov 16 '18 at 11:08
Anthony Winzlet
18.2k42345
18.2k42345
asked Nov 16 '18 at 9:06
mhndevmhndev
6581127
6581127
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You can use the aggregation pipeline to first group all actions into one array plus the associated user id into objects and the filter to have action 1
, but not 2
, then keep only the id:
db.collection.aggregate([
// group user id and all actions together
$group:
_id: "$user_id",
actions:
$addToSet: "$action"
,
// filter documents which have 1 as action but not 2
$match:
$and: [
"actions": 1
,
"actions":
$not:
$eq: 2
]
,
// only keep the id
$group:
_id: "$_id"
])
This returns:
[
"_id": 8
,
"_id": 9
]
Here is a link to a playgorund: https://mongoplayground.net/p/So4HjEXx3sn
You should consider how you structure your documents. Your design looks a bit like a relational database. It is advisable to model your documents according to your read accesses (if possible). In this case you could an user_id
and an actions
field, which has already all action ids grouped together.
thanks for your good answer.
– mhndev
Nov 16 '18 at 10:17
add a comment |
You can use $group
and $push
the actions
for the distinct user_id
. Finally use $match
with actions
$eq
ual to 1 but not equal ($ne
) to 2
db.collection.aggregate([
"$group":
"_id": "$user_id",
"actions": "$push": "$action"
,
"$match": "actions": "$eq": 1, "$ne": 2 ,
{ "$project": "_id": 1
])
thanks, I thinks both answers are correct, so I chose the first answer.
– mhndev
Nov 16 '18 at 10:18
1
yes But you don't need to use two$group
stages here. Simply use$project
because we already find the distinctuser_id
in the first$group
stage.
– Anthony Winzlet
Nov 16 '18 at 10:28
yes that's right
– mhndev
Nov 16 '18 at 10:45
1
I have updated the answer. Even that it has less operators. You don't need to use$group
,$and
,$not
aggregation here.
– Anthony Winzlet
Nov 16 '18 at 17:10
yes, I noticed, thanks
– mhndev
Nov 16 '18 at 17:13
add a 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%2f53334567%2fmongodb-where-not-exists-subtract-two-query-results%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use the aggregation pipeline to first group all actions into one array plus the associated user id into objects and the filter to have action 1
, but not 2
, then keep only the id:
db.collection.aggregate([
// group user id and all actions together
$group:
_id: "$user_id",
actions:
$addToSet: "$action"
,
// filter documents which have 1 as action but not 2
$match:
$and: [
"actions": 1
,
"actions":
$not:
$eq: 2
]
,
// only keep the id
$group:
_id: "$_id"
])
This returns:
[
"_id": 8
,
"_id": 9
]
Here is a link to a playgorund: https://mongoplayground.net/p/So4HjEXx3sn
You should consider how you structure your documents. Your design looks a bit like a relational database. It is advisable to model your documents according to your read accesses (if possible). In this case you could an user_id
and an actions
field, which has already all action ids grouped together.
thanks for your good answer.
– mhndev
Nov 16 '18 at 10:17
add a comment |
You can use the aggregation pipeline to first group all actions into one array plus the associated user id into objects and the filter to have action 1
, but not 2
, then keep only the id:
db.collection.aggregate([
// group user id and all actions together
$group:
_id: "$user_id",
actions:
$addToSet: "$action"
,
// filter documents which have 1 as action but not 2
$match:
$and: [
"actions": 1
,
"actions":
$not:
$eq: 2
]
,
// only keep the id
$group:
_id: "$_id"
])
This returns:
[
"_id": 8
,
"_id": 9
]
Here is a link to a playgorund: https://mongoplayground.net/p/So4HjEXx3sn
You should consider how you structure your documents. Your design looks a bit like a relational database. It is advisable to model your documents according to your read accesses (if possible). In this case you could an user_id
and an actions
field, which has already all action ids grouped together.
thanks for your good answer.
– mhndev
Nov 16 '18 at 10:17
add a comment |
You can use the aggregation pipeline to first group all actions into one array plus the associated user id into objects and the filter to have action 1
, but not 2
, then keep only the id:
db.collection.aggregate([
// group user id and all actions together
$group:
_id: "$user_id",
actions:
$addToSet: "$action"
,
// filter documents which have 1 as action but not 2
$match:
$and: [
"actions": 1
,
"actions":
$not:
$eq: 2
]
,
// only keep the id
$group:
_id: "$_id"
])
This returns:
[
"_id": 8
,
"_id": 9
]
Here is a link to a playgorund: https://mongoplayground.net/p/So4HjEXx3sn
You should consider how you structure your documents. Your design looks a bit like a relational database. It is advisable to model your documents according to your read accesses (if possible). In this case you could an user_id
and an actions
field, which has already all action ids grouped together.
You can use the aggregation pipeline to first group all actions into one array plus the associated user id into objects and the filter to have action 1
, but not 2
, then keep only the id:
db.collection.aggregate([
// group user id and all actions together
$group:
_id: "$user_id",
actions:
$addToSet: "$action"
,
// filter documents which have 1 as action but not 2
$match:
$and: [
"actions": 1
,
"actions":
$not:
$eq: 2
]
,
// only keep the id
$group:
_id: "$_id"
])
This returns:
[
"_id": 8
,
"_id": 9
]
Here is a link to a playgorund: https://mongoplayground.net/p/So4HjEXx3sn
You should consider how you structure your documents. Your design looks a bit like a relational database. It is advisable to model your documents according to your read accesses (if possible). In this case you could an user_id
and an actions
field, which has already all action ids grouped together.
answered Nov 16 '18 at 9:55
mbuechmannmbuechmann
3,07121429
3,07121429
thanks for your good answer.
– mhndev
Nov 16 '18 at 10:17
add a comment |
thanks for your good answer.
– mhndev
Nov 16 '18 at 10:17
thanks for your good answer.
– mhndev
Nov 16 '18 at 10:17
thanks for your good answer.
– mhndev
Nov 16 '18 at 10:17
add a comment |
You can use $group
and $push
the actions
for the distinct user_id
. Finally use $match
with actions
$eq
ual to 1 but not equal ($ne
) to 2
db.collection.aggregate([
"$group":
"_id": "$user_id",
"actions": "$push": "$action"
,
"$match": "actions": "$eq": 1, "$ne": 2 ,
{ "$project": "_id": 1
])
thanks, I thinks both answers are correct, so I chose the first answer.
– mhndev
Nov 16 '18 at 10:18
1
yes But you don't need to use two$group
stages here. Simply use$project
because we already find the distinctuser_id
in the first$group
stage.
– Anthony Winzlet
Nov 16 '18 at 10:28
yes that's right
– mhndev
Nov 16 '18 at 10:45
1
I have updated the answer. Even that it has less operators. You don't need to use$group
,$and
,$not
aggregation here.
– Anthony Winzlet
Nov 16 '18 at 17:10
yes, I noticed, thanks
– mhndev
Nov 16 '18 at 17:13
add a comment |
You can use $group
and $push
the actions
for the distinct user_id
. Finally use $match
with actions
$eq
ual to 1 but not equal ($ne
) to 2
db.collection.aggregate([
"$group":
"_id": "$user_id",
"actions": "$push": "$action"
,
"$match": "actions": "$eq": 1, "$ne": 2 ,
{ "$project": "_id": 1
])
thanks, I thinks both answers are correct, so I chose the first answer.
– mhndev
Nov 16 '18 at 10:18
1
yes But you don't need to use two$group
stages here. Simply use$project
because we already find the distinctuser_id
in the first$group
stage.
– Anthony Winzlet
Nov 16 '18 at 10:28
yes that's right
– mhndev
Nov 16 '18 at 10:45
1
I have updated the answer. Even that it has less operators. You don't need to use$group
,$and
,$not
aggregation here.
– Anthony Winzlet
Nov 16 '18 at 17:10
yes, I noticed, thanks
– mhndev
Nov 16 '18 at 17:13
add a comment |
You can use $group
and $push
the actions
for the distinct user_id
. Finally use $match
with actions
$eq
ual to 1 but not equal ($ne
) to 2
db.collection.aggregate([
"$group":
"_id": "$user_id",
"actions": "$push": "$action"
,
"$match": "actions": "$eq": 1, "$ne": 2 ,
{ "$project": "_id": 1
])
You can use $group
and $push
the actions
for the distinct user_id
. Finally use $match
with actions
$eq
ual to 1 but not equal ($ne
) to 2
db.collection.aggregate([
"$group":
"_id": "$user_id",
"actions": "$push": "$action"
,
"$match": "actions": "$eq": 1, "$ne": 2 ,
{ "$project": "_id": 1
])
edited Nov 16 '18 at 17:07
answered Nov 16 '18 at 9:57
Anthony WinzletAnthony Winzlet
18.2k42345
18.2k42345
thanks, I thinks both answers are correct, so I chose the first answer.
– mhndev
Nov 16 '18 at 10:18
1
yes But you don't need to use two$group
stages here. Simply use$project
because we already find the distinctuser_id
in the first$group
stage.
– Anthony Winzlet
Nov 16 '18 at 10:28
yes that's right
– mhndev
Nov 16 '18 at 10:45
1
I have updated the answer. Even that it has less operators. You don't need to use$group
,$and
,$not
aggregation here.
– Anthony Winzlet
Nov 16 '18 at 17:10
yes, I noticed, thanks
– mhndev
Nov 16 '18 at 17:13
add a comment |
thanks, I thinks both answers are correct, so I chose the first answer.
– mhndev
Nov 16 '18 at 10:18
1
yes But you don't need to use two$group
stages here. Simply use$project
because we already find the distinctuser_id
in the first$group
stage.
– Anthony Winzlet
Nov 16 '18 at 10:28
yes that's right
– mhndev
Nov 16 '18 at 10:45
1
I have updated the answer. Even that it has less operators. You don't need to use$group
,$and
,$not
aggregation here.
– Anthony Winzlet
Nov 16 '18 at 17:10
yes, I noticed, thanks
– mhndev
Nov 16 '18 at 17:13
thanks, I thinks both answers are correct, so I chose the first answer.
– mhndev
Nov 16 '18 at 10:18
thanks, I thinks both answers are correct, so I chose the first answer.
– mhndev
Nov 16 '18 at 10:18
1
1
yes But you don't need to use two
$group
stages here. Simply use $project
because we already find the distinct user_id
in the first $group
stage.– Anthony Winzlet
Nov 16 '18 at 10:28
yes But you don't need to use two
$group
stages here. Simply use $project
because we already find the distinct user_id
in the first $group
stage.– Anthony Winzlet
Nov 16 '18 at 10:28
yes that's right
– mhndev
Nov 16 '18 at 10:45
yes that's right
– mhndev
Nov 16 '18 at 10:45
1
1
I have updated the answer. Even that it has less operators. You don't need to use
$group
, $and
, $not
aggregation here.– Anthony Winzlet
Nov 16 '18 at 17:10
I have updated the answer. Even that it has less operators. You don't need to use
$group
, $and
, $not
aggregation here.– Anthony Winzlet
Nov 16 '18 at 17:10
yes, I noticed, thanks
– mhndev
Nov 16 '18 at 17:13
yes, I noticed, thanks
– mhndev
Nov 16 '18 at 17:13
add a 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%2f53334567%2fmongodb-where-not-exists-subtract-two-query-results%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