Search text with mongodb regex with boolean operations
https://www.mycompiler.io/view/DK9xGoUmf0d
db.students.insertMany([
{ id: 1, name: 'Ryan', gender: 'M' },
{ id: 2, name: 'Joanna', gender: 'F' },
{ id: 1, name: 'Test Ryan', gender: 'M' },
{ id: 1, name: 'this C++ test', gender: 'M' }
]);
{ out : "\nStart with word test" };
db.students.find({ name: {
"$regex": /^jo/i
} });
db.students.find({ name: {
"$regex": /^Ryan$/
} });
db.students.find({ name: {
"$regex": /^Rya$/
} });
{ out : "\nc++ test" };
db.students.find({ name: {
"$regex": /c\+\+/i
} });
{ out : "\nand operator test" };
db.students.find({ name: {
"$regex": "(?=.*this)(?=.*test)"
} });
{ out : "\nand operator test ignore case" };
db.students.find({ name: {
"$regex": /((?=.*This)(?=.*Test))/i
} });
{ out : "\nor test" };
db.students.find({ name: {
"$regex": /(this | tost)/i
} });
db.students.find({ name: {
"$regex": /(^test | tost)/i
} });
{ out : "\nand with c++ test" };
db.students.find({ name: {
"$regex": /(c\+\+) (?=.*Test)/i
} });
No comments:
Post a Comment