Native mongodb queries within payload

default discord avatar
gak4u
4 months ago
8

Can I write native mongodb queries within payload without creating new connection? I would like to do some aggregations and also do some count queries.

  • default discord avatar
    paulpopus
    4 months ago

    Hey @gak4u , I couldn't find the docs page for this but you have direct access to the mongoose model inside the local API via

    collections.slug.Model


    Does this fit within your needs?

    image.png
  • default discord avatar
    gak4u
    4 months ago

    @paulpopus you are lifesaver!!

  • default discord avatar
    jarrod69420
    4 months ago

    Yep you have complete access to the models like @paulpopus described.



    When accessing a model directly just know that things like access-control & hooks will not run (which is fine). Only

    payload.

    operations (create/read/update/delete etc.) trigger hooks and access control to run.

  • default discord avatar
    ssyberg
    3 months ago

    Is there any way to access the mongo connection NOT through a model (e.g. so you could run database level operations?)

  • discord user avatar
    jmikrut
    Payload Team
    3 months ago

    that's a question for mongoose honestly but it's a good question



    i bet mongoose has some way of doing that



    payload itself doesn't though

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.