Given that new Documents and querys:
{
"_id" : ObjectId("55803fd1cc0c9aa090e608be"),
"song_name" : "Mickey is good",
"time" : "3.56",
"artist" : "Mickey"
}
{
"_id" : ObjectId("55803fd1cc0c9aa090e608bf"),
"song_name" : "Love is nothing",
"time" : "5.56",
"artist" : "Jimmy"
}
I need to find which songs have a title identical to the artist name. i have tried: db.myCollection.find( { $where: "this.artist == /this.song_name/" } );
but did not work out. please help`
If I understand it well, you need to find all songs whose title contains the name of the artist. You don't need a regular expression for that1. As simple test using
indexOfwould suffice. Given your sample data:If you really need to find the songs whose title is equal to the artist name (you don't have such a document in your sample data), you would simply write:
Please note in either cases, I've normalized the strings using
toLowerCase(). Depending your use case, you might need something much more sophisticated than that. For example, if you need to match "You and Me" by "You+Me".1Using a regular expression here might even be harmful if the artist name contains meta-characters or quantifiers. For example, given the artist name "You+Me", the
+here would be interpreted as 1 or more occurrence of the preceding character. So, matching "YouMe", "YouuuuuMe" but not the raw string "You+Me".