Database

You can interact with your database with normal MongoDB commands. Make sure you select the Database checkbox which will import the db at the top of your file:

import { db } from("swizzle-js");

The db variable is a pre-connected instance of your mongo database. You can make any MongoDB query on it, for example:

  • Find - Retrieves all documents from the students collection where the grade is "senior".

    const seniorStudents = await db.collection('students').find({ grade: "senior" }).toArray();

  • Update - Updates the first document in the students collection with the name "Alice" to set her age to 26.

    const updateResult = await db.collection('students').updateOne({ name: "Alice" }, { $set: { age: 26 } });

  • Insert - Inserts a new user document into the students collection.

    const newStudent = {
        name: "Bob",
        age: 18,
        email: "[email protected]"
    };
    const insertResult = await db.collection('students').insertOne(newStudent);

  • Count - Counts the number of documents in the students collection where the age is greater than 18.

    const count = await db.collection('students').countDocuments({ age: { $gt: 20 } });

Last updated