1+ /* Text search */
2+
13const { MongoClient } = require ( "mongodb" ) ;
24
3- // Replace the following string with your MongoDB deployment's connection string.
4- const uri =
5- "mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority" ;
5+ // Replace the following string with your MongoDB deployment's connection string
6+ const uri = "mongodb+srv://<user>:<password>@<cluster-url>?writeConcern=majority" ;
67const client = new MongoClient ( uri ) ;
78
89async function word ( movies ) {
910 // start word text example
11+ // Create a query that searches for the string "trek"
1012 const query = { $text : { $search : "trek" } } ;
1113
1214 // Return only the `title` of each matched document
@@ -15,21 +17,23 @@ async function word(movies) {
1517 title : 1 ,
1618 } ;
1719
18- // find documents based on our query and projection
20+ // Find documents based on our query and projection
1921 const cursor = movies . find ( query ) . project ( projection ) ;
2022 // end word text example
2123
22- // print a message if no documents were found
24+ // Print a message if no documents were found
2325 if ( ( await movies . countDocuments ( query ) ) === 0 ) {
2426 console . log ( "No documents found!" ) ;
2527 }
28+ // Print all documents that were found
2629 for await ( const doc of cursor ) {
2730 console . dir ( doc ) ;
2831 }
2932}
3033
3134async function phrase ( movies ) {
3235 // start phrase text example
36+ // Create a query that searches for the phrase "star trek"
3337 const query = { $text : { $search : "\"star trek\"" } } ;
3438
3539 // Return only the `title` of each matched document
@@ -38,21 +42,23 @@ async function phrase(movies) {
3842 title : 1 ,
3943 } ;
4044
41- // find documents based on our query and projection
45+ // Find documents based on the query and projection
4246 const cursor = movies . find ( query ) . project ( projection ) ;
4347 // end phrase text example
4448
45- // print a message if no documents were found
49+ // Print a message if no documents were found
4650 if ( ( await movies . countDocuments ( query ) ) === 0 ) {
4751 console . log ( "No documents found!" ) ;
4852 }
53+ // Print all documents that were found
4954 for await ( const doc of cursor ) {
5055 console . dir ( doc ) ;
5156 }
5257}
5358
5459async function negation ( movies ) {
5560 // start negation text example
61+ // Create a query that searches for the phrase "star trek" while omitting "into darkness"
5662 const query = { $text : { $search : "\"star trek\" -\"into darkness\"" } } ;
5763
5864 // Include only the `title` field of each matched document
@@ -61,43 +67,47 @@ async function negation(movies) {
6167 title : 1 ,
6268 } ;
6369
64- // find documents based on our query and projection
70+ // Find documents based on the query and projection
6571 const cursor = movies . find ( query ) . project ( projection ) ;
6672 // end negation text example
6773
68- // print a message if no documents were found
74+ // Print a message if no documents were found
6975 if ( ( await movies . countDocuments ( query ) ) === 0 ) {
7076 console . log ( "No documents found!" ) ;
7177 }
78+ // Print all documents that were found
7279 for await ( const doc of cursor ) {
7380 console . dir ( doc ) ;
7481 }
7582}
7683
7784async function relevance ( movies ) {
7885 // start relevance text example
86+ // Create a query that searches for the phrase "star trek" while omitting "into darkness"r
7987 const query = { $text : { $search : "\"star trek\" -\"into darkness\"" } } ;
8088
81- // sort returned documents by descending text relevance score
89+ // Sort returned documents by descending text relevance score
8290 const sort = { score : { $meta : "textScore" } } ;
91+
8392 // Include only the `title` and `score` fields in each returned document
8493 const projection = {
8594 _id : 0 ,
8695 title : 1 ,
8796 score : { $meta : "textScore" } ,
8897 } ;
8998
90- // find documents based on our query, sort, and projection
99+ // Find documents based on the query, sort, and projection
91100 const cursor = movies
92101 . find ( query )
93102 . sort ( sort )
94103 . project ( projection ) ;
95104 // end relevance text example
96105
97- // print a message if no documents were found
106+ // Print a message if no documents were found
98107 if ( ( await movies . countDocuments ( query ) ) === 0 ) {
99108 console . log ( "No documents found!" ) ;
100109 }
110+ // Print all documents that were found
101111 for await ( const doc of cursor ) {
102112 console . dir ( doc ) ;
103113 }
@@ -113,6 +123,7 @@ async function run() {
113123 await negation ( movies ) ;
114124 await relevance ( movies ) ;
115125 } finally {
126+ // Close the database connection on completion or error
116127 await client . close ( ) ;
117128 }
118129}
0 commit comments