Evgeniy Lenskiy
Evgeniy Lenskiy

Reputation: 408

Sequelize find in JSON field

I have PostgreSQL database with JSON type field named "data" that has following content structure:

{
  "requestData" : {
    "url": "some url"
    "body": {
      "page_id": 12
    }
  }
}

I try to make findAll query request with filter by page_id using Sequelize, but don't get some results.

The question is: could I search by nested field in JSON type, or only in JSONB type? And how?

Upvotes: 17

Views: 20651

Answers (1)

Mahdi
Mahdi

Reputation: 1405

According to this link You can approach the answer via 3 ways:

Nested Key

{
  "meta.audio.length": {
    [Op.gt]: 20
  }
}

Nested object

{
  meta: {
    video: {
      url: {
        [Op.ne]: null
      }
    }
  }
}

Containment

{
  "meta": {
    [Op.contains]: {
      site: {
        url: 'http://google.com'
      }
    }
  }
}

Upvotes: 21

Related Questions