-
Notifications
You must be signed in to change notification settings - Fork 0
/
textfind.js
52 lines (43 loc) · 1.37 KB
/
textfind.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// ignored first line
const { MongoClient } = require("mongodb");
// Replace the following with your MongoDB deployment's connection
// string.
const uri =
"mongodb+srv://admin:[email protected]/test?retryWrites=true&w=majority";
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function run() {
try {
await client.connect();
const database = client.db("sample_mflix");
const collection = database.collection("users");
const query = { $text: { $search: "stark" } };
// sort returned documents by descending text relevence score
const sort = { score: { $meta: "textScore" } };
// Include only the `title` and `imdb` fields in each returned document
const projection = {
_id: 0,
name: 1,
email: 1,
score: { $meta: "textScore" },
};
// find documents based on our query, sort, and projection
const cursor = collection
.find(query)
.sort(sort)
.project(projection);
//uncomment these two lines to see strange behavior:
//const docs = await cursor.toArray();
//console.log(docs);
// print a message if no documents were found
if (!(await cursor.hasNext())) {
console.log("No documents found!");
}
await cursor.forEach(console.dir);
} finally {
await client.close();
}
}
run().catch(console.dir);