Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      CodeSOD: A Unique Way to Primary Key

      July 22, 2025

      BrowserStack launches Figma plugin for detecting accessibility issues in design phase

      July 22, 2025

      Parasoft brings agentic AI to service virtualization in latest release

      July 22, 2025

      Node.js vs. Python for Backend: 7 Reasons C-Level Leaders Choose Node.js Talent

      July 21, 2025

      The best CRM software with email marketing in 2025: Expert tested and reviewed

      July 22, 2025

      This multi-port car charger can power 4 gadgets at once – and it’s surprisingly cheap

      July 22, 2025

      I’m a wearables editor and here are the 7 Pixel Watch 4 rumors I’m most curious about

      July 22, 2025

      8 ways I quickly leveled up my Linux skills – and you can too

      July 22, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025
      Recent

      The Intersection of Agile and Accessibility – A Series on Designing for Everyone

      July 22, 2025

      Zero Trust & Cybersecurity Mesh: Your Org’s Survival Guide

      July 22, 2025

      Execute Ping Commands and Get Back Structured Data in PHP

      July 22, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025
      Recent

      A Tomb Raider composer has been jailed — His legacy overshadowed by $75k+ in loan fraud

      July 22, 2025

      “I don’t think I changed his mind” — NVIDIA CEO comments on H20 AI GPU sales resuming in China following a meeting with President Trump

      July 22, 2025

      Galaxy Z Fold 7 review: Six years later — Samsung finally cracks the foldable code

      July 22, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Databases»Mongoose Now Natively Supports QE and CSFLE

    Mongoose Now Natively Supports QE and CSFLE

    June 2, 2025

    Mongoose 8.15.0 has been released, which adds support for the industry-leading encryption solutions available from MongoDB. With this update, it’s simpler than ever to create documents leveraging MongoDB Queryable Encryption (QE) and Client-Side Level Field Encryption (CSFLE), keeping your data secure when it is in use. Read on to learn more about approaches to encrypting your data when building with MongoDB and Mongoose.

    What is Mongoose?

    Mongoose is a library that enables elegant object modeling for Node.js applications working with MongoDB. Similar to an Object-Relational Mapper (ORM), the Mongoose Object Document Mapper (ODM) simplifies programmatic data interaction through schemas and models. It allows developers to define data structures with validation and provides a rich API for CRUD operations, abstracting away many of the complexities of the underlying MongoDB driver. This integration enhances productivity by enabling developers to work with JavaScript objects instead of raw database queries, making it easier to manage data relationships and enforce data integrity.

    What is QE and CSFLE?

    Securing sensitive data is paramount. It must be protected at every stage—whether in transit, at rest, or in use. However, implementing in-use encryption can be complex. MongoDB offers two approaches to make it easier: Queryable Encryption (QE) and Client-Side Level Field Encryption (CSFLE). QE allows customers to encrypt sensitive application data, store it securely in an encrypted state in the MongoDB database, and perform equality and range queries directly on the encrypted data.

    An industry-first innovation, QE eliminates the need for costly custom encryption solutions, complex third-party tools, or specialized cryptography knowledge. It employs a unique structured encryption schema, developed by the MongoDB Cryptography Research Group, that simplifies the encryption of sensitive data while enabling equality and range queries to be performed directly on data without having to decrypt it.

    The data remains encrypted at all stages, with decryption occurring only on the client side. This architecture supports solidified strict access controls, where MongoDB and even an organization’s own database administrators (DBAs) don’t have access to sensitive data. This design enhances security by keeping the server unaware of the data it processes, further mitigating the risk of exposure and minimizing the potential for unauthorized access.

    Adding QE/CSFLE auto-encryption support for Mongoose

    The primary goal of the Mongoose integration with QE and CSFLE is to provide idiomatic support for automatic encryption, simplifying the process of creating encrypted models. With native support for QE and CSFLE, Mongoose allows developers to define encryption options directly within their schemas without the need for separate configurations. This first-class API enables developers to work within Mongoose without dropping down to the driver level, minimizing the need for significant code changes when adopting QE and CSFLE.

    Mongoose streamlines configuration by automatically generating the encrypted field map. This ensures that encrypted fields align perfectly with the schema and simplifies the three-step process typically associated with encryption setup, shown below. Mongoose also keeps the schema and encrypted fields in sync, reducing the risk of mismatches.

    Developers can easily declare fields with the encrypt property and configure encryption settings, using all field types and encryption schemes supported by QE and CSFLE. Additionally, users can manage their own encryption keys, enhancing control over their encryption processes. This comprehensive approach empowers developers to implement robust encryption effortlessly while maintaining operational efficiency.

    Pre-integration experience

    const kmsProviders = { &NewLine;  local: { key: Buffer.alloc(96)&NewLine;};&NewLine;const keyVaultNamespace = 'data.keys';&NewLine;const extraOptions = {};&NewLine;const encryptedDatabaseName = 'encrypted';&NewLine;const uri = '<mongodb URI>';&NewLine;&NewLine;const encryptedFieldsMap = {&NewLine;  'encrypted.patent': {&NewLine;    encryptedFields: EJSON.parse('<EJSON string containing encrypted fields, either output from manual creation or createEncryptedCollection>', { relaxed: false }),&NewLine;  }&NewLine;};&NewLine;&NewLine;const autoEncryptionOptions = {&NewLine;  keyVaultNamespace,&NewLine;  kmsProviders,&NewLine;  extraOptions,&NewLine;  encryptedFieldsMap&NewLine;};&NewLine;&NewLine;const schema = new Schema({&NewLine;  patientName: String,&NewLine;  patientId: Number,&NewLine;  field: String,&NewLine;  patientRecord: {&NewLine;    ssn: String,&NewLine;    billing: String&NewLine;  }&NewLine;}, { collection: 'patent' });&NewLine;&NewLine;const connection = await createConnection(uri, {&NewLine;  dbName: encryptedDatabaseName,&NewLine;  autoEncryption: autoEncryptionOptions,&NewLine;  autoCreate: false, // If using createEncryptedCollection, this is false.  If manually creating the keyIds for each field, this is true.&NewLine;}).asPromise();&NewLine;const PatentModel = connection.model('Patent', schema);&NewLine;&NewLine;const result = await PatentModel.find({}).exec();&NewLine;console.log(result);&NewLine;

    This example demonstrates the manual configuration required to set up a Mongoose model for QE and CSFLE, requiring three different steps to:

    • Define an encryptedFieldsMap to specify which fields to encrypt

    • Configure autoEncryptionOptions with key management settings
      Create a Mongoose connection that incorporates these options

    This process can be cumbersome, as it requires explicit setup for encryption.

    New experience with Mongoose 8.15.0

    const schema = new Schema({&NewLine;  patientName: String,&NewLine;  patientId: Number,&NewLine;  field: String,&NewLine;  patientRecord: {&NewLine;    ssn: {&NewLine;      type: String,&NewLine;      encrypt: {&NewLine;        keyId: '<uuid string of key id>',&NewLine;        queries: 'equality'&NewLine;      }&NewLine;    },&NewLine;    billing: {&NewLine;      type: String,&NewLine;      encrypt: {&NewLine;        keyId: '<uuid string of key id>',&NewLine;        queries: 'equality'&NewLine;      }&NewLine;    },&NewLine;  }&NewLine;}, { encryptionType: 'queryableEncryption', collection: 'patent' });&NewLine;&NewLine;const connection = mongoose.createConnection();&NewLine;&NewLine;const PatentModel = connection.model('Patent', schema);&NewLine;&NewLine;const keyVaultNamespace = 'client.encryption';&NewLine;const kmsProviders = {   local: { key: Buffer.alloc(96) };&NewLine;const uri = '<mongodb URI>';&NewLine;const keyVaultNamespace = 'data.keys';&NewLine;const autoEncryptionOptions = {&NewLine;  keyVaultNamespace, &NewLine;  kmsProviders,&NewLine;  extraOptions: {}&NewLine;};&NewLine;&NewLine;await connection.openUri(uri, {&NewLine;  autoEncryption: autoEncryptionOptions});&NewLine;&NewLine;const result = await PatentModel.find({}).exec();&NewLine;console.log(result);&NewLine;

    This “after experience” example showcases how the integration of QE and CSFLE into Mongoose simplifies the encryption setup process. Instead of the previous three-step approach, developers can now define encryption directly within the schema.

    In this implementation, fields like ssn and billing are marked with an encrypt property, allowing for straightforward configuration of encryption settings, including the keyId and query types. The connection to the database is established with a single call that includes the necessary auto-encryption options, eliminating the need for a separate encrypted fields map and complex configurations. This streamlined approach enables developers to work natively within Mongoose, enhancing usability and reducing setup complexity while maintaining robust encryption capabilities.

    Learn more about QE/CSFLE for Mongoose

    We’re excited for you to build secure applications with QE/CSFLE for Mongoose. Here are some resources to get started with:

    • Learn how to set up use Mongoose with MongoDB through our tutorial.

    • Check out our documentation to learn when to choose QE vs. CSFLE.

    • Read Mongoose CSFLE documentation.

    Source: Read More

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleMore Mainframe Trailblazer Trivia
    Next Article TempoMail — Command Line Temporary Email in Linux

    Related Posts

    Development

    GPT-5 is Coming: Revolutionizing Software Testing

    July 22, 2025
    Development

    Win the Accessibility Game: Combining AI with Human Judgment

    July 22, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    Microsoft Store adds Copilot, smarter search, and a personalized homepage

    Operating Systems

    Meeting summarization and action item extraction with Amazon Nova

    Machine Learning

    Trump’s AI-generated papal portrait sparks controversy and debate

    Artificial Intelligence

    CVE-2025-30391 – Microsoft Dynamics Information Disclosure Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    CVE-2025-43020 – Poly Clariti Manager Command Injection

    July 22, 2025

    CVE ID : CVE-2025-43020

    Published : July 22, 2025, 11:15 p.m. | 1 hour, 21 minutes ago

    Description : A potential command
    injection vulnerability has been identified in the Poly Clariti Manager for
    versions prior to 10.12.2. The vulnerability could allow a privileged user
    to submit arbitrary input. HP has addressed the issue in the latest software update.

    Severity: 0.0 | NA

    Visit the link for more details, such as CVSS details, affected products, timeline, and more…

    Long-Context Multimodal Understanding No Longer Requires Massive Models: NVIDIA AI Introduces Eagle 2.5, a Generalist Vision-Language Model that Matches GPT-4o on Video Tasks Using Just 8B Parameters

    April 22, 2025

    Microsoft Edge will lose all these features this month

    May 13, 2025

    Stanford Researchers Propose FramePack: A Compression-based AI Framework to Tackle Drifting and Forgetting in Long-Sequence Video Generation Using Efficient Context Management and Sampling

    April 21, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.