If I've understood your question correctly I beleieve the below $regex is what you're after.
My collection looks like this:
/* 1 */
{
"_id" : ObjectId("5a8498a29d1ed018c7f648ca"),
"name" : "John, Smith, "
}
The find and $regex looks like :
db.foo.find({ name: { $regex: 'John.*Smith' } }, { _id : 0 })
If you needed case insensitivity:
db.foo.find({ name: { $regex: 'john.*smith', $options: 'i'} }, { _id : 0 })
Output:
/* 1 */
{
"name" : "John, Smith, "
}
If I was to run:
db.foo.find( { name: { $regex: 'Bill.*Smith', $options: 'i' }}, { _id : 0})
or
db.foo.find( { name: { $regex: 'John.*Bill', $options: 'i' } }, { _id : 0})
Output:
Fetched 0 record(s) in 1ms
So the $regex will only return a match if John AND Smith are in the field.
To elaborate on the actual $regex itself:
. Matches any single character except the newline character
* Matches the preceding expression 0 or more times
$option i is for case insensitivity