How do i return an array of sub documents in mongoose (with criteria)? [duplicate]
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?
mongodb mongoose
marked as duplicate by Neil Lunn
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.
add a comment |
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?
mongodb mongoose
marked as duplicate by Neil Lunn
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
add a comment |
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?
mongodb mongoose
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
mongodb mongoose
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
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
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
add a comment |
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
add a comment |
0
active
oldest
votes
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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