How to find all entries by a certain key?

Hi all, I’m new to how this all works so forgive me if I am in the wrong place…

I am trying to fetch all entries in my collection by a field I’ve added in the schema called ‘userId’.

I know I can use find() method but I can’t find a good solution.

This is one example entry in my collection:

_id: ObjectId('658b748a043ebec7bb5eaddf')
title: "Shoulders"
userId: "658b31d497187d9cf9b3f00c"
__v: 0

So, my question is, how can I fetch all entries with a specific “userId” with Express, React, axios and MongoDB?

This is my groupController:

// Fetch group endpoint
const fetchGroups = async (req, res) => {
    const id = req.params;  // I don't think this is correct
    try {
        // Group is from my models folder
        const foundData = await Group.find({userId: id}).toArray(); // Nor this

        if (!foundData) {
            return res.status(404).json({ message: 'Data not found' });
        }

        res.status(200).json(foundData);
    } catch (error) {
        console.log(error);
    }
}

This is my groupRoutes file:

router.get('/fetch-groups/:id', fetchGroups);

This is my axios request in React:

const fetchGroups = async () => {
        try {
            const groups = await axios.get(`/groups/fetch-groups/${user?.id}`) // user.id is passed from context provider
            setData(groups);
        } catch (error) {
            console.log(error);
        }
    }

Any help would be greatly appreciated, thank you :slight_smile:

this looks ok if…
if Group is derived/defined something like this:
const uri = “”;
const client = new MongoClient(uri);
const database = client.db(“myDB”);
const Group = database.collection(“myCollection”);

and id is set to your desired string like “658b31d497187d9cf9b3f00c”