Query on Embedded Array Of Document in mongo based on multiple fields in spring [duplicate]











up vote
0
down vote

favorite













This question already has an answer here:




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

    11 answers



  • Create filter aggregation in spring

    1 answer




I have my data in below mongo structure:



{
id:ObjectId("xyz")
name:"aaa"
>list:Array
>0:Object
fname:"John1",
lname:"Doe1"
>1:Object
fname:"John2",
lname:"Doe2"
},
{
id:ObjectId("abc")
name:"bbb"
>list:Array
>0:Object
fname:"John3",
lname:"Doe3"
>1:Object
fname:"John2",
lname:"Doe2"
}


I want to retrieve those list items which matches fname and lname and along with name. I have tried below methods but none of them is working :



1.{'list.fname':'John2'} -- It's showing all the main documents even it is 
matching with one list item
2. Tried using { list: {$elemMatch: {'fname': 'John2', 'lname': 'Doe2' }}}
3. ({'list.fname':'John2'},{"name"=1,list.$=1})
4.{$match:{'list.fname':'John2'}},{$unwind:"$list"},
{$match:'list.lname':'Doe2'}}


None of the above worked.Also tried few example from the stackoverflow , but didn't worked. I am using mongo 3.6. I also tried using method query in mongo spring data , nothing worked .Can somebody help out.










share|improve this question













marked as duplicate by Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework 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 20 at 1:31


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.















  • The $elemMatch is actually the correct form to match the "document", but you appear to be expecting only the matched array element, which of course has been answered before. Also "Does not work" is not really descriptive, and just posting little snippets of code from other answers does not really mean anything to us. If you state exactly why it does not work and what you are not getting and what you expect as a result, then we can clearly see what you are asking. You likely mean this though, because it's what everybody else asks for and expects.
    – Neil Lunn
    Nov 20 at 1:35















up vote
0
down vote

favorite













This question already has an answer here:




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

    11 answers



  • Create filter aggregation in spring

    1 answer




I have my data in below mongo structure:



{
id:ObjectId("xyz")
name:"aaa"
>list:Array
>0:Object
fname:"John1",
lname:"Doe1"
>1:Object
fname:"John2",
lname:"Doe2"
},
{
id:ObjectId("abc")
name:"bbb"
>list:Array
>0:Object
fname:"John3",
lname:"Doe3"
>1:Object
fname:"John2",
lname:"Doe2"
}


I want to retrieve those list items which matches fname and lname and along with name. I have tried below methods but none of them is working :



1.{'list.fname':'John2'} -- It's showing all the main documents even it is 
matching with one list item
2. Tried using { list: {$elemMatch: {'fname': 'John2', 'lname': 'Doe2' }}}
3. ({'list.fname':'John2'},{"name"=1,list.$=1})
4.{$match:{'list.fname':'John2'}},{$unwind:"$list"},
{$match:'list.lname':'Doe2'}}


None of the above worked.Also tried few example from the stackoverflow , but didn't worked. I am using mongo 3.6. I also tried using method query in mongo spring data , nothing worked .Can somebody help out.










share|improve this question













marked as duplicate by Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework 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 20 at 1:31


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.















  • The $elemMatch is actually the correct form to match the "document", but you appear to be expecting only the matched array element, which of course has been answered before. Also "Does not work" is not really descriptive, and just posting little snippets of code from other answers does not really mean anything to us. If you state exactly why it does not work and what you are not getting and what you expect as a result, then we can clearly see what you are asking. You likely mean this though, because it's what everybody else asks for and expects.
    – Neil Lunn
    Nov 20 at 1:35













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




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

    11 answers



  • Create filter aggregation in spring

    1 answer




I have my data in below mongo structure:



{
id:ObjectId("xyz")
name:"aaa"
>list:Array
>0:Object
fname:"John1",
lname:"Doe1"
>1:Object
fname:"John2",
lname:"Doe2"
},
{
id:ObjectId("abc")
name:"bbb"
>list:Array
>0:Object
fname:"John3",
lname:"Doe3"
>1:Object
fname:"John2",
lname:"Doe2"
}


I want to retrieve those list items which matches fname and lname and along with name. I have tried below methods but none of them is working :



1.{'list.fname':'John2'} -- It's showing all the main documents even it is 
matching with one list item
2. Tried using { list: {$elemMatch: {'fname': 'John2', 'lname': 'Doe2' }}}
3. ({'list.fname':'John2'},{"name"=1,list.$=1})
4.{$match:{'list.fname':'John2'}},{$unwind:"$list"},
{$match:'list.lname':'Doe2'}}


None of the above worked.Also tried few example from the stackoverflow , but didn't worked. I am using mongo 3.6. I also tried using method query in mongo spring data , nothing worked .Can somebody help out.










share|improve this question














This question already has an answer here:




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

    11 answers



  • Create filter aggregation in spring

    1 answer




I have my data in below mongo structure:



{
id:ObjectId("xyz")
name:"aaa"
>list:Array
>0:Object
fname:"John1",
lname:"Doe1"
>1:Object
fname:"John2",
lname:"Doe2"
},
{
id:ObjectId("abc")
name:"bbb"
>list:Array
>0:Object
fname:"John3",
lname:"Doe3"
>1:Object
fname:"John2",
lname:"Doe2"
}


I want to retrieve those list items which matches fname and lname and along with name. I have tried below methods but none of them is working :



1.{'list.fname':'John2'} -- It's showing all the main documents even it is 
matching with one list item
2. Tried using { list: {$elemMatch: {'fname': 'John2', 'lname': 'Doe2' }}}
3. ({'list.fname':'John2'},{"name"=1,list.$=1})
4.{$match:{'list.fname':'John2'}},{$unwind:"$list"},
{$match:'list.lname':'Doe2'}}


None of the above worked.Also tried few example from the stackoverflow , but didn't worked. I am using mongo 3.6. I also tried using method query in mongo spring data , nothing worked .Can somebody help out.





This question already has an answer here:




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

    11 answers



  • Create filter aggregation in spring

    1 answer








arrays mongodb spring-data-jpa mongodb-query aggregation-framework






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 21:56









619

113




113




marked as duplicate by Neil Lunn aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework 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 20 at 1:31


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 aggregation-framework
Users with the  aggregation-framework badge can single-handedly close aggregation-framework 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 20 at 1:31


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.














  • The $elemMatch is actually the correct form to match the "document", but you appear to be expecting only the matched array element, which of course has been answered before. Also "Does not work" is not really descriptive, and just posting little snippets of code from other answers does not really mean anything to us. If you state exactly why it does not work and what you are not getting and what you expect as a result, then we can clearly see what you are asking. You likely mean this though, because it's what everybody else asks for and expects.
    – Neil Lunn
    Nov 20 at 1:35


















  • The $elemMatch is actually the correct form to match the "document", but you appear to be expecting only the matched array element, which of course has been answered before. Also "Does not work" is not really descriptive, and just posting little snippets of code from other answers does not really mean anything to us. If you state exactly why it does not work and what you are not getting and what you expect as a result, then we can clearly see what you are asking. You likely mean this though, because it's what everybody else asks for and expects.
    – Neil Lunn
    Nov 20 at 1:35
















The $elemMatch is actually the correct form to match the "document", but you appear to be expecting only the matched array element, which of course has been answered before. Also "Does not work" is not really descriptive, and just posting little snippets of code from other answers does not really mean anything to us. If you state exactly why it does not work and what you are not getting and what you expect as a result, then we can clearly see what you are asking. You likely mean this though, because it's what everybody else asks for and expects.
– Neil Lunn
Nov 20 at 1:35




The $elemMatch is actually the correct form to match the "document", but you appear to be expecting only the matched array element, which of course has been answered before. Also "Does not work" is not really descriptive, and just posting little snippets of code from other answers does not really mean anything to us. If you state exactly why it does not work and what you are not getting and what you expect as a result, then we can clearly see what you are asking. You likely mean this though, because it's what everybody else asks for and expects.
– Neil Lunn
Nov 20 at 1:35

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen