Querying data
The first thing you'll want to do with your data is query it. To do so, you will need to start by connecting to your database.
Connecting to your database
In your CodeSanbox, on line 5 of server/index.mjs
, you will see a variable called uri
.
You will need to add your connection string to this variable. You can find your connection string by going to your database in the MongoDB Atlas dashboard. From there, do the same thing you did earlier in the Prepare Your Data section to get your connection string.
Once you have your connection string, paste it in your CodeSanbox.
Lines 5-12 should look like this:
const uri = "mongodb+srv://user:password@cluster0.r0doihh.mongodb.net/?retryWrites=true&w=majority";
// Create a MongoClient that will manage the connection to the database.
const client = new MongoClient(uri);
await client.connect();
await client.db("admin").command({ ping: 1 });
console.log("Pinged your deployment. You successfully connected to MongoDB!");
const collection = client.db("soccer").collection("players");
This code takes your connection string and creates a MongoClient
object. This object is used to connect to your database.
It also pings the database to make sure you are connected. If you are connected, you will see the message Pinged your deployment. You successfully connected to MongoDB!
in your console.
Finally, it declares a collection called collection
that you will use to query your data.
Querying your data
The first thing we'll do is to query the data using a find
method. This method will return all the documents in your collection matching the query.
In your CodeSanbox, on line 24 of server/index.mjs
. This is the code that will be processed when a GET request is received at the /find/:name
on the server.
Add the following code.
const query = { short_name: "L. Messi" };
Now go to the MongoDB documentation and find out how to query your data using the find
method.
Once you have the code, add it to your CodeSanbox.
Answer
app.get("/find/:name", async (req, res) => {
const query = { short_name: "L. Messi" };
const players = await collection
.find(query)
.toArray();
res.json(players).status(200);
});
Test it out
Great! You've just queried your data. Now let's test it out.
In your CodeSanbox, look at the client application. Type something in the input box, and click on the "Score" button.
You should see the data about Lionel Messi show up on the page.
Querying your data with a parameter
That's great, but you don't want to query the same player every time. You want to be able to query any player.
The text from the input box is sent to the server as a parameter. You can access it using req.params.name
.
Can you change the code so that the query uses the parameter instead of the hardcoded value?
Answer
app.get("/find/:name", async (req, res) => {
const query = { short_name: req.params.name };
const players = await collection
.find(query)
.toArray();
res.json(players).status(200);
});
Test it out -- and see the limitations
You can now enter data in the search box and search for players. Try searching for L. Messi
or F. Duffy
. Those should return a player.
Now try a more broad search like Messi
. You should see an empty array.
This is because the find
method is looking for an exact match. If you want to search for a partial match, you will need to use a different method.