How do i return an array of sub documents in mongoose (with criteria)? [duplicate]












0
















This question already has an answer here:




  • Text search to return only relevant subdocuments

    1 answer



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

    11 answers




I have a userSchema with an array of subdocuments using mongoose.



A simplified form looks like this:



let userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
trim: true
},
friends: [{
name: String,
description, String
}]
)}


I have added a compound full text search index to the friends subdocument (name and description).



When I try to get specific friends with a criteria, as expected, the documents that have friends with that criteria are returned.



return await User.find(
{ $text: { $search: 'some_string'} },
{ friends: true, _id: false}
)


So the above code returns complete arrays of friends in the documents that fit the criteria.



What i'm trying to figure out is if it's possible to get only specific friends in a specific document.



In my head, the code might look something like this:



let user = await User.findById('some_id');
user.find(
{ friends: {$text: { $search: 'some_string'}}},
{ friends: true, _id: false}
)


Note: I know that the above code would never work.



I'm basically trying to get a smaller specific array of friends in this one document based on some criteria.



Is this possible?










share|improve this question















marked as duplicate by Neil Lunn mongoose
Users with the  mongoose badge can single-handedly close mongoose 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 21 '18 at 6:26


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.



















  • Even though this is a "true array" unlke the data shown in the linked question, the answer remains the same. Text searches cannot identify array elements in which they matched. It's possible with regular expressions or a combination of in order to actually filter the array elements.

    – Neil Lunn
    Nov 21 '18 at 6:28
















0
















This question already has an answer here:




  • Text search to return only relevant subdocuments

    1 answer



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

    11 answers




I have a userSchema with an array of subdocuments using mongoose.



A simplified form looks like this:



let userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
trim: true
},
friends: [{
name: String,
description, String
}]
)}


I have added a compound full text search index to the friends subdocument (name and description).



When I try to get specific friends with a criteria, as expected, the documents that have friends with that criteria are returned.



return await User.find(
{ $text: { $search: 'some_string'} },
{ friends: true, _id: false}
)


So the above code returns complete arrays of friends in the documents that fit the criteria.



What i'm trying to figure out is if it's possible to get only specific friends in a specific document.



In my head, the code might look something like this:



let user = await User.findById('some_id');
user.find(
{ friends: {$text: { $search: 'some_string'}}},
{ friends: true, _id: false}
)


Note: I know that the above code would never work.



I'm basically trying to get a smaller specific array of friends in this one document based on some criteria.



Is this possible?










share|improve this question















marked as duplicate by Neil Lunn mongoose
Users with the  mongoose badge can single-handedly close mongoose 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 21 '18 at 6:26


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.



















  • Even though this is a "true array" unlke the data shown in the linked question, the answer remains the same. Text searches cannot identify array elements in which they matched. It's possible with regular expressions or a combination of in order to actually filter the array elements.

    – Neil Lunn
    Nov 21 '18 at 6:28














0












0








0









This question already has an answer here:




  • Text search to return only relevant subdocuments

    1 answer



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

    11 answers




I have a userSchema with an array of subdocuments using mongoose.



A simplified form looks like this:



let userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
trim: true
},
friends: [{
name: String,
description, String
}]
)}


I have added a compound full text search index to the friends subdocument (name and description).



When I try to get specific friends with a criteria, as expected, the documents that have friends with that criteria are returned.



return await User.find(
{ $text: { $search: 'some_string'} },
{ friends: true, _id: false}
)


So the above code returns complete arrays of friends in the documents that fit the criteria.



What i'm trying to figure out is if it's possible to get only specific friends in a specific document.



In my head, the code might look something like this:



let user = await User.findById('some_id');
user.find(
{ friends: {$text: { $search: 'some_string'}}},
{ friends: true, _id: false}
)


Note: I know that the above code would never work.



I'm basically trying to get a smaller specific array of friends in this one document based on some criteria.



Is this possible?










share|improve this question

















This question already has an answer here:




  • Text search to return only relevant subdocuments

    1 answer



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

    11 answers




I have a userSchema with an array of subdocuments using mongoose.



A simplified form looks like this:



let userSchema = new mongoose.Schema({
email: {
type: String,
unique: true,
trim: true
},
friends: [{
name: String,
description, String
}]
)}


I have added a compound full text search index to the friends subdocument (name and description).



When I try to get specific friends with a criteria, as expected, the documents that have friends with that criteria are returned.



return await User.find(
{ $text: { $search: 'some_string'} },
{ friends: true, _id: false}
)


So the above code returns complete arrays of friends in the documents that fit the criteria.



What i'm trying to figure out is if it's possible to get only specific friends in a specific document.



In my head, the code might look something like this:



let user = await User.findById('some_id');
user.find(
{ friends: {$text: { $search: 'some_string'}}},
{ friends: true, _id: false}
)


Note: I know that the above code would never work.



I'm basically trying to get a smaller specific array of friends in this one document based on some criteria.



Is this possible?





This question already has an answer here:




  • Text search to return only relevant subdocuments

    1 answer



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

    11 answers








mongodb mongoose






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 6:26









Neil Lunn

98.1k23174184




98.1k23174184










asked Nov 21 '18 at 5:51









VictorVictor

12




12




marked as duplicate by Neil Lunn mongoose
Users with the  mongoose badge can single-handedly close mongoose 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 21 '18 at 6:26


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 mongoose
Users with the  mongoose badge can single-handedly close mongoose 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 21 '18 at 6:26


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.















  • Even though this is a "true array" unlke the data shown in the linked question, the answer remains the same. Text searches cannot identify array elements in which they matched. It's possible with regular expressions or a combination of in order to actually filter the array elements.

    – Neil Lunn
    Nov 21 '18 at 6:28



















  • Even though this is a "true array" unlke the data shown in the linked question, the answer remains the same. Text searches cannot identify array elements in which they matched. It's possible with regular expressions or a combination of in order to actually filter the array elements.

    – Neil Lunn
    Nov 21 '18 at 6:28

















Even though this is a "true array" unlke the data shown in the linked question, the answer remains the same. Text searches cannot identify array elements in which they matched. It's possible with regular expressions or a combination of in order to actually filter the array elements.

– Neil Lunn
Nov 21 '18 at 6:28





Even though this is a "true array" unlke the data shown in the linked question, the answer remains the same. Text searches cannot identify array elements in which they matched. It's possible with regular expressions or a combination of in order to actually filter the array elements.

– Neil Lunn
Nov 21 '18 at 6:28












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]