1
+ /* Text search */
2
+
1
3
const { MongoClient } = require ( "mongodb" ) ;
2
4
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" ;
6
7
const client = new MongoClient ( uri ) ;
7
8
8
9
async function word ( movies ) {
9
10
// start word text example
11
+ // Create a query that searches for the string "trek"
10
12
const query = { $text : { $search : "trek" } } ;
11
13
12
14
// Return only the `title` of each matched document
@@ -15,21 +17,23 @@ async function word(movies) {
15
17
title : 1 ,
16
18
} ;
17
19
18
- // find documents based on our query and projection
20
+ // Find documents based on our query and projection
19
21
const cursor = movies . find ( query ) . project ( projection ) ;
20
22
// end word text example
21
23
22
- // print a message if no documents were found
24
+ // Print a message if no documents were found
23
25
if ( ( await movies . countDocuments ( query ) ) === 0 ) {
24
26
console . log ( "No documents found!" ) ;
25
27
}
28
+ // Print all documents that were found
26
29
for await ( const doc of cursor ) {
27
30
console . dir ( doc ) ;
28
31
}
29
32
}
30
33
31
34
async function phrase ( movies ) {
32
35
// start phrase text example
36
+ // Create a query that searches for the phrase "star trek"
33
37
const query = { $text : { $search : "\"star trek\"" } } ;
34
38
35
39
// Return only the `title` of each matched document
@@ -38,21 +42,23 @@ async function phrase(movies) {
38
42
title : 1 ,
39
43
} ;
40
44
41
- // find documents based on our query and projection
45
+ // Find documents based on the query and projection
42
46
const cursor = movies . find ( query ) . project ( projection ) ;
43
47
// end phrase text example
44
48
45
- // print a message if no documents were found
49
+ // Print a message if no documents were found
46
50
if ( ( await movies . countDocuments ( query ) ) === 0 ) {
47
51
console . log ( "No documents found!" ) ;
48
52
}
53
+ // Print all documents that were found
49
54
for await ( const doc of cursor ) {
50
55
console . dir ( doc ) ;
51
56
}
52
57
}
53
58
54
59
async function negation ( movies ) {
55
60
// start negation text example
61
+ // Create a query that searches for the phrase "star trek" while omitting "into darkness"
56
62
const query = { $text : { $search : "\"star trek\" -\"into darkness\"" } } ;
57
63
58
64
// Include only the `title` field of each matched document
@@ -61,43 +67,47 @@ async function negation(movies) {
61
67
title : 1 ,
62
68
} ;
63
69
64
- // find documents based on our query and projection
70
+ // Find documents based on the query and projection
65
71
const cursor = movies . find ( query ) . project ( projection ) ;
66
72
// end negation text example
67
73
68
- // print a message if no documents were found
74
+ // Print a message if no documents were found
69
75
if ( ( await movies . countDocuments ( query ) ) === 0 ) {
70
76
console . log ( "No documents found!" ) ;
71
77
}
78
+ // Print all documents that were found
72
79
for await ( const doc of cursor ) {
73
80
console . dir ( doc ) ;
74
81
}
75
82
}
76
83
77
84
async function relevance ( movies ) {
78
85
// start relevance text example
86
+ // Create a query that searches for the phrase "star trek" while omitting "into darkness"r
79
87
const query = { $text : { $search : "\"star trek\" -\"into darkness\"" } } ;
80
88
81
- // sort returned documents by descending text relevance score
89
+ // Sort returned documents by descending text relevance score
82
90
const sort = { score : { $meta : "textScore" } } ;
91
+
83
92
// Include only the `title` and `score` fields in each returned document
84
93
const projection = {
85
94
_id : 0 ,
86
95
title : 1 ,
87
96
score : { $meta : "textScore" } ,
88
97
} ;
89
98
90
- // find documents based on our query, sort, and projection
99
+ // Find documents based on the query, sort, and projection
91
100
const cursor = movies
92
101
. find ( query )
93
102
. sort ( sort )
94
103
. project ( projection ) ;
95
104
// end relevance text example
96
105
97
- // print a message if no documents were found
106
+ // Print a message if no documents were found
98
107
if ( ( await movies . countDocuments ( query ) ) === 0 ) {
99
108
console . log ( "No documents found!" ) ;
100
109
}
110
+ // Print all documents that were found
101
111
for await ( const doc of cursor ) {
102
112
console . dir ( doc ) ;
103
113
}
@@ -113,6 +123,7 @@ async function run() {
113
123
await negation ( movies ) ;
114
124
await relevance ( movies ) ;
115
125
} finally {
126
+ // Close the database connection on completion or error
116
127
await client . close ( ) ;
117
128
}
118
129
}
0 commit comments