Q3
  • Introduction
  • Related Projects
  • Roadmap
  • API
    • Core
      • Rest
      • Session
      • Schema plugins
    • Schemas
      • Rates
      • Rebates
      • Permissions
    • Utils
      • Subdocuments
      • Soft deleting
  • Client
    • gatsby-theme-q3
    • q3-components
      • Quantity
    • q3-ui-cart
      • Drawer
    • Admin
      • Builders
      • Containers
        • Contexts
    • Datatables
      • TableBadge
    • Forms
      • Field
      • Presets
        • <PasswordChange />
    • Repeater
    • UI
      • CallToAction
      • Avatar
      • CollapsiblePanel
      • SubMenu
Powered by GitBook
On this page
  • API reference
  • Example code

Was this helpful?

  1. API
  2. Utils

Subdocuments

Q3 registers a global mongoose plugin for easier subdocument management.

API reference

Method

Description

Parameters

getSubDocument

Lookup a single sub-document by its ID

field (String), id (String)

pushSubDocument

Add to a new or existing embedded array

field (String), document (Object)

removeSubDocument

Remove a single or set of sub-documents

field (String), id (String or Array)

updateSubDocument

Update part of a sub-document record

field (String), id (String), arguments (Object)

Example code

// Let's make John
const doc = Model.create({ 
    name: "John",
});

// John finds a new friend
const { friends: [{ _id }] } = await doc.pushSubDocument('friends', { 
    name: "Jen",
});

// John's friend changes her name
await doc.updateSubDocument('friends', _id, { name: "Jennifer" });

// John no longer has any friends.
await doc.removeSubDocument('friends', _id);

PreviousUtilsNextSoft deleting

Last updated 5 years ago

Was this helpful?