I have the following Mongoose Schema:
var UserSchema = new Schema({
name: String,
age: Number,
...
tags: [{
text: String,
...
}]
});
and the following array:
var tagTexts = ['tall', 'small', 'green', 'blue'];
I would like to retrieve all user documents that contain at least one tag with a text property found within tagTexts.
For example, if I had the following users and tagTexts array
[{
name: 'Bob',
age: 17,
...
tags: [{
text: 'small',
...
}]
}, {
name: 'Bill',
age: 29,
...
tags: [{
text: 'dandelion',
...
}, {
text: 'oak',
...
}]
}]
var tagTexts = ['tall', 'small', 'green', 'blue'];
then Bob would be retrieved, but not Bill.
You can use $in
to match against an array of values, and dot notation to target the text
field within each element of the tags
array. Any match will cause a doc to be included in the results.
var tagTexts = ['tall', 'small', 'green', 'blue'];
User.find({'tags.text': {$in: tagTexts}}, function(err, users) {...});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With