Mongodb find() where a query matches has two matches on an object [duplicate]












0
















This question already has an answer here:




  • MongoDB : find value in Array with multiple criteria

    1 answer



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers



  • Aggregate Count Array Members Matching Condition

    1 answer




I'm new to Mongo and am having issues constructing a query!



Data:



{
"_id" : 1,
"ActiveState" : {
"_id" : 1,
"Products" : [
{
"ProductId" : 17,
"Active" : true,
{
"ProductId" : 17,
"Active" : false,
},
{
"ProductId" : 19,
"Active" : true
},
{
"ProductId" : 19,
"Active" : false,
},
],
},
},
{
"_id" : 2,
"ActiveState" : {
"_id" : 1,
"Products" : [
{
"ProductId" : 17,
"Active" : true,
{
"ProductId" : 17,
"Active" : true,
},
{
"ProductId" : 19,
"Active" : true
},
{
"ProductId" : 19,
"Active" : false,
},
],
},
}


Query:



db.getCollection('Asset').find( { $and: [{'ActiveState.Products.ProductId': 17, 'ActiveState.Products.Active':true}] } )



Query Intent:
I would like to find all instances where the object has more than one object in the Products array that meets:



ActiveState.Products.ProductId = 17 AND ActiveState.Products.Active = true;



I would only like object _id: 2 to be returned because it has two Products with ProductId 17 and Active true.



Many thanks










share|improve this question













marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • db.getCollection('Asset').find({ 'ActiveState.Products': { '$elemMatch': { 'ProductId': 17, 'Active':true } } }) in case any of that is unclear. Also covered in the core documentation under Specify Multiple Conditions for Array of Documents. The $filter examples on the other question are for when you decide you just want the matching array elements.

    – Neil Lunn
    Nov 23 '18 at 10:46











  • Hi Neil, unfortunately, that yields the same results as my query. I need match more than one rather than just at least one. Thanks for the links to the docs but I don't see how to craft the query I need (or I'm misunderstanding the docs). Also, could you let me know how this is a duplicate? I can't find a duplicate on Stack Exchange! Thanks for your help!

    – A Dyer
    Nov 23 '18 at 11:13











  • So, you think there's not a single one eh?. Like I already said. The $elemMatch is because your "query" attempts are incorrect, and read the documentation link to find out why. The "aggregate $filter" stuff is for your "matching elements". The $size in the new existing answer shows getting the size of the filtered array. And then all you need to do is $match on the projected size so that the only documents have 2.

    – Neil Lunn
    Nov 23 '18 at 11:23













  • Yeah, I'm sorry I don't think I've explained it clearly so apologies about that. I wouldn't expect the first object to be returned as it has one Product Id 17 with Active False and one with Product Id 17 with Active True. However, the second object has two Product Id 17 and Active true. The query currently returns both but I only want the second object. Thanks very much for your help :)

    – A Dyer
    Nov 23 '18 at 11:26


















0
















This question already has an answer here:




  • MongoDB : find value in Array with multiple criteria

    1 answer



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers



  • Aggregate Count Array Members Matching Condition

    1 answer




I'm new to Mongo and am having issues constructing a query!



Data:



{
"_id" : 1,
"ActiveState" : {
"_id" : 1,
"Products" : [
{
"ProductId" : 17,
"Active" : true,
{
"ProductId" : 17,
"Active" : false,
},
{
"ProductId" : 19,
"Active" : true
},
{
"ProductId" : 19,
"Active" : false,
},
],
},
},
{
"_id" : 2,
"ActiveState" : {
"_id" : 1,
"Products" : [
{
"ProductId" : 17,
"Active" : true,
{
"ProductId" : 17,
"Active" : true,
},
{
"ProductId" : 19,
"Active" : true
},
{
"ProductId" : 19,
"Active" : false,
},
],
},
}


Query:



db.getCollection('Asset').find( { $and: [{'ActiveState.Products.ProductId': 17, 'ActiveState.Products.Active':true}] } )



Query Intent:
I would like to find all instances where the object has more than one object in the Products array that meets:



ActiveState.Products.ProductId = 17 AND ActiveState.Products.Active = true;



I would only like object _id: 2 to be returned because it has two Products with ProductId 17 and Active true.



Many thanks










share|improve this question













marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • db.getCollection('Asset').find({ 'ActiveState.Products': { '$elemMatch': { 'ProductId': 17, 'Active':true } } }) in case any of that is unclear. Also covered in the core documentation under Specify Multiple Conditions for Array of Documents. The $filter examples on the other question are for when you decide you just want the matching array elements.

    – Neil Lunn
    Nov 23 '18 at 10:46











  • Hi Neil, unfortunately, that yields the same results as my query. I need match more than one rather than just at least one. Thanks for the links to the docs but I don't see how to craft the query I need (or I'm misunderstanding the docs). Also, could you let me know how this is a duplicate? I can't find a duplicate on Stack Exchange! Thanks for your help!

    – A Dyer
    Nov 23 '18 at 11:13











  • So, you think there's not a single one eh?. Like I already said. The $elemMatch is because your "query" attempts are incorrect, and read the documentation link to find out why. The "aggregate $filter" stuff is for your "matching elements". The $size in the new existing answer shows getting the size of the filtered array. And then all you need to do is $match on the projected size so that the only documents have 2.

    – Neil Lunn
    Nov 23 '18 at 11:23













  • Yeah, I'm sorry I don't think I've explained it clearly so apologies about that. I wouldn't expect the first object to be returned as it has one Product Id 17 with Active False and one with Product Id 17 with Active True. However, the second object has two Product Id 17 and Active true. The query currently returns both but I only want the second object. Thanks very much for your help :)

    – A Dyer
    Nov 23 '18 at 11:26
















0












0








0









This question already has an answer here:




  • MongoDB : find value in Array with multiple criteria

    1 answer



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers



  • Aggregate Count Array Members Matching Condition

    1 answer




I'm new to Mongo and am having issues constructing a query!



Data:



{
"_id" : 1,
"ActiveState" : {
"_id" : 1,
"Products" : [
{
"ProductId" : 17,
"Active" : true,
{
"ProductId" : 17,
"Active" : false,
},
{
"ProductId" : 19,
"Active" : true
},
{
"ProductId" : 19,
"Active" : false,
},
],
},
},
{
"_id" : 2,
"ActiveState" : {
"_id" : 1,
"Products" : [
{
"ProductId" : 17,
"Active" : true,
{
"ProductId" : 17,
"Active" : true,
},
{
"ProductId" : 19,
"Active" : true
},
{
"ProductId" : 19,
"Active" : false,
},
],
},
}


Query:



db.getCollection('Asset').find( { $and: [{'ActiveState.Products.ProductId': 17, 'ActiveState.Products.Active':true}] } )



Query Intent:
I would like to find all instances where the object has more than one object in the Products array that meets:



ActiveState.Products.ProductId = 17 AND ActiveState.Products.Active = true;



I would only like object _id: 2 to be returned because it has two Products with ProductId 17 and Active true.



Many thanks










share|improve this question















This question already has an answer here:




  • MongoDB : find value in Array with multiple criteria

    1 answer



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers



  • Aggregate Count Array Members Matching Condition

    1 answer




I'm new to Mongo and am having issues constructing a query!



Data:



{
"_id" : 1,
"ActiveState" : {
"_id" : 1,
"Products" : [
{
"ProductId" : 17,
"Active" : true,
{
"ProductId" : 17,
"Active" : false,
},
{
"ProductId" : 19,
"Active" : true
},
{
"ProductId" : 19,
"Active" : false,
},
],
},
},
{
"_id" : 2,
"ActiveState" : {
"_id" : 1,
"Products" : [
{
"ProductId" : 17,
"Active" : true,
{
"ProductId" : 17,
"Active" : true,
},
{
"ProductId" : 19,
"Active" : true
},
{
"ProductId" : 19,
"Active" : false,
},
],
},
}


Query:



db.getCollection('Asset').find( { $and: [{'ActiveState.Products.ProductId': 17, 'ActiveState.Products.Active':true}] } )



Query Intent:
I would like to find all instances where the object has more than one object in the Products array that meets:



ActiveState.Products.ProductId = 17 AND ActiveState.Products.Active = true;



I would only like object _id: 2 to be returned because it has two Products with ProductId 17 and Active true.



Many thanks





This question already has an answer here:




  • MongoDB : find value in Array with multiple criteria

    1 answer



  • Retrieve only the queried element in an object array in MongoDB collection

    11 answers



  • Aggregate Count Array Members Matching Condition

    1 answer








mongodb mongodb-query






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 10:35









A DyerA Dyer

134




134




marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 23 '18 at 10:37


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • db.getCollection('Asset').find({ 'ActiveState.Products': { '$elemMatch': { 'ProductId': 17, 'Active':true } } }) in case any of that is unclear. Also covered in the core documentation under Specify Multiple Conditions for Array of Documents. The $filter examples on the other question are for when you decide you just want the matching array elements.

    – Neil Lunn
    Nov 23 '18 at 10:46











  • Hi Neil, unfortunately, that yields the same results as my query. I need match more than one rather than just at least one. Thanks for the links to the docs but I don't see how to craft the query I need (or I'm misunderstanding the docs). Also, could you let me know how this is a duplicate? I can't find a duplicate on Stack Exchange! Thanks for your help!

    – A Dyer
    Nov 23 '18 at 11:13











  • So, you think there's not a single one eh?. Like I already said. The $elemMatch is because your "query" attempts are incorrect, and read the documentation link to find out why. The "aggregate $filter" stuff is for your "matching elements". The $size in the new existing answer shows getting the size of the filtered array. And then all you need to do is $match on the projected size so that the only documents have 2.

    – Neil Lunn
    Nov 23 '18 at 11:23













  • Yeah, I'm sorry I don't think I've explained it clearly so apologies about that. I wouldn't expect the first object to be returned as it has one Product Id 17 with Active False and one with Product Id 17 with Active True. However, the second object has two Product Id 17 and Active true. The query currently returns both but I only want the second object. Thanks very much for your help :)

    – A Dyer
    Nov 23 '18 at 11:26





















  • db.getCollection('Asset').find({ 'ActiveState.Products': { '$elemMatch': { 'ProductId': 17, 'Active':true } } }) in case any of that is unclear. Also covered in the core documentation under Specify Multiple Conditions for Array of Documents. The $filter examples on the other question are for when you decide you just want the matching array elements.

    – Neil Lunn
    Nov 23 '18 at 10:46











  • Hi Neil, unfortunately, that yields the same results as my query. I need match more than one rather than just at least one. Thanks for the links to the docs but I don't see how to craft the query I need (or I'm misunderstanding the docs). Also, could you let me know how this is a duplicate? I can't find a duplicate on Stack Exchange! Thanks for your help!

    – A Dyer
    Nov 23 '18 at 11:13











  • So, you think there's not a single one eh?. Like I already said. The $elemMatch is because your "query" attempts are incorrect, and read the documentation link to find out why. The "aggregate $filter" stuff is for your "matching elements". The $size in the new existing answer shows getting the size of the filtered array. And then all you need to do is $match on the projected size so that the only documents have 2.

    – Neil Lunn
    Nov 23 '18 at 11:23













  • Yeah, I'm sorry I don't think I've explained it clearly so apologies about that. I wouldn't expect the first object to be returned as it has one Product Id 17 with Active False and one with Product Id 17 with Active True. However, the second object has two Product Id 17 and Active true. The query currently returns both but I only want the second object. Thanks very much for your help :)

    – A Dyer
    Nov 23 '18 at 11:26



















db.getCollection('Asset').find({ 'ActiveState.Products': { '$elemMatch': { 'ProductId': 17, 'Active':true } } }) in case any of that is unclear. Also covered in the core documentation under Specify Multiple Conditions for Array of Documents. The $filter examples on the other question are for when you decide you just want the matching array elements.

– Neil Lunn
Nov 23 '18 at 10:46





db.getCollection('Asset').find({ 'ActiveState.Products': { '$elemMatch': { 'ProductId': 17, 'Active':true } } }) in case any of that is unclear. Also covered in the core documentation under Specify Multiple Conditions for Array of Documents. The $filter examples on the other question are for when you decide you just want the matching array elements.

– Neil Lunn
Nov 23 '18 at 10:46













Hi Neil, unfortunately, that yields the same results as my query. I need match more than one rather than just at least one. Thanks for the links to the docs but I don't see how to craft the query I need (or I'm misunderstanding the docs). Also, could you let me know how this is a duplicate? I can't find a duplicate on Stack Exchange! Thanks for your help!

– A Dyer
Nov 23 '18 at 11:13





Hi Neil, unfortunately, that yields the same results as my query. I need match more than one rather than just at least one. Thanks for the links to the docs but I don't see how to craft the query I need (or I'm misunderstanding the docs). Also, could you let me know how this is a duplicate? I can't find a duplicate on Stack Exchange! Thanks for your help!

– A Dyer
Nov 23 '18 at 11:13













So, you think there's not a single one eh?. Like I already said. The $elemMatch is because your "query" attempts are incorrect, and read the documentation link to find out why. The "aggregate $filter" stuff is for your "matching elements". The $size in the new existing answer shows getting the size of the filtered array. And then all you need to do is $match on the projected size so that the only documents have 2.

– Neil Lunn
Nov 23 '18 at 11:23







So, you think there's not a single one eh?. Like I already said. The $elemMatch is because your "query" attempts are incorrect, and read the documentation link to find out why. The "aggregate $filter" stuff is for your "matching elements". The $size in the new existing answer shows getting the size of the filtered array. And then all you need to do is $match on the projected size so that the only documents have 2.

– Neil Lunn
Nov 23 '18 at 11:23















Yeah, I'm sorry I don't think I've explained it clearly so apologies about that. I wouldn't expect the first object to be returned as it has one Product Id 17 with Active False and one with Product Id 17 with Active True. However, the second object has two Product Id 17 and Active true. The query currently returns both but I only want the second object. Thanks very much for your help :)

– A Dyer
Nov 23 '18 at 11:26







Yeah, I'm sorry I don't think I've explained it clearly so apologies about that. I wouldn't expect the first object to be returned as it has one Product Id 17 with Active False and one with Product Id 17 with Active True. However, the second object has two Product Id 17 and Active true. The query currently returns both but I only want the second object. Thanks very much for your help :)

– A Dyer
Nov 23 '18 at 11:26














0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin