Unlocking the power of JSONB column filtering in KNEX with Typescript/Javascript: A comprehensive guide

Could someone please assist me with this query issue? I have a table with a jsonb column that stores stringified data in the following format:

entry: {
    1: "data1",
    2: "data2"
}

I am trying to retrieve entries where the key is 1 and its value is "data1". However, my current query is throwing an SQL error:

sqlMessage: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '['1'] = 'data1' limit 500' at line 1",

This is how the query looks in my model:

return await db(db_table).select('*')
.limit(per_page).where('form_id', form_id)
.whereRaw("entry->>[?] = ?",['1', 'data1']); // issue is here

Can anyone suggest the correct way to construct the last line in order to search the json field/column and return all matching rows from the table where key 1 equals data1? Thank you!

Answer №1

This is the expected format:

await db.select('*')
  .limit(per_page).where('form_id', form_id)
  .whereRaw("entry->$.?? = ?",['1', 'data1']); // the error lies here
  // ------------------^ this extra ? is a placeholder for a column value

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Executing multiple HTTP requests simultaneously in groups using an asynchronous for loop for each individual request

I'm currently working on running multiple requests simultaneously in batches to an API using an array of keywords. Read Denis Fatkhudinov's article for more insights. The issue I'm facing involves rerunning the request for each keyword with ...

Deploying a React App to Github Pages Results in a Blank Screen

After successfully uploading a simple react app onto GitHub pages using gh-pages, I attempted to import the Shards dashboard project (https://github.com/DesignRevision/shards-dashboard-react) onto my GitHub page. Unfortunately, all I see is a blank page. ...

What is the correct way to access $auth in Nuxt with TypeScript?

<script lang="ts"> import LoginAdmin from '@/components/LoginAdmin.vue' import { Component, Vue } from 'nuxt-property-decorator' import Auth from "@nuxtjs/auth"; export default class MyStore extends Vue { pub ...

The issue of batch-wise data clustering on Google Maps when zooming in or out

//The code snippet provided below initializes a map with specified settings and clusters markers based on the data sent in patches of 10000.// var mapDiv = document.getElementById('newmap'); map = new google.maps.Map(mapDiv, { center ...

The Django application is failing to interact with the AJAX autocomplete functionality

After typing the term "bi" into the search bar, I expected to see a username starting with those initials displayed in a dropdown list. However, nothing is showing up. Here are the codes I have used: search.html <html> <div class="ui-widget"> ...

Unselecting a span in AngularJS: Switching selection to another span

Whenever I click on an item from my list displayed using ngrepeat, it generates another list specific to the selected element. My goal is to change the background color of the clicked element to indicate it has been selected. However, the problem arises wh ...

Is it acceptable to incorporate Node.js modules for utilization in Next.js?

Here's a funny question for you. I am trying to generate UUID in my Next.js project without adding any unnecessary packages. So, I decided to import crypto in my component like this: import crypto from 'crypto'; After importing it, I used i ...

Interactions of selecting dates in datepicker widget

Currently, I am working on developing my personal work website. One issue I have encountered is with the calendar feature. The "From" date can be selected before today's date, which is not ideal. Additionally, if a date 5 days from now is chosen as th ...

Static variable storing instances of objects

I am faced with the following scenario: function Configuration() { } Configuration.users = { 'user1': new User() } The users variable is a static member of Configuration and I am attempting to have it store an instance of a User object. Howev ...

Creating a dynamic textarea input based on the number entered using AngularJS

Can a loop be simplified in AngularJs to easily determine the number of textarea inputs a user can fill? <input type="number" class="form-control" min="1" max="4" value="1"> <div ng-repeat="..."> <div class="form-group"> < ...

In Internet Explorer 8, angular's $window service may sometimes return undefined

I've developed a custom directive called slideshow with some scope variables that are two-way bound to ng-style. This directive functions as a responsive carousel that adjusts based on the window height retrieved using the $window service. During tes ...

Adding an object to a scene in Three.js

I've been experimenting with a code example I found online and my goal is to incorporate a textured cube into the project while specifying its position. However, despite my efforts, the cube doesn't seem to be showing up. Here's what I have ...

Click event triggering smooth scrolling

I am currently working on implementing a smooth scrolling effect using the React JavaScript library without relying on jQuery. My goal is to ensure that when a user clicks on a link, they are directed to the specific section of the page seamlessly. The f ...

Creating a dashed line for a circle in SVG by using the path element

Currently, I am facing a challenge where I need to create a circle using multiple points that are very close together. For reference, you can view the jsfiddle link provided below: http://jsfiddle.net/L6e5oz3L/ <path style="" stroke-dasharray="10 5" f ...

Struggling with implementing the group by feature in AngularJS?

Currently, I am trying to group a select-window by 2 different object arrays - subcategories and rootcategories. Each subcategory has a relation id that links to the rootcategory's id. My goal is to have the dropdown window group the subcategories bas ...

Tracking the number of images and adjusting the counter as the user scrolls

I am working on a project where I have a series of images displayed in a column. I would like to add a dynamic counter to indicate which image is currently in focus based on the scroll position. Feel free to check out the demo <div class="counter"> ...

Display the variable on the document by clicking the button

Hello, I'm new here so please forgive me if I make any mistakes. I am working with the following PHP code: <?php $quoteFile = "quotes.txt"; //Store quotes in this file $fp = fopen($quoteFile, "r"); //Open file for reading $content = fread ...

What is the best approach to dynamically implement useReducer in code?

Take a look at the repository here: https://github.com/charles7771/ugly-code In the 'Options' component, I am facing an issue where I am hardcoding different names for each reducer case instead of dynamically generating them for a form. This app ...

Could you please explain the meaning of "rc" in the names of configuration files?

Many tools in the modern JavaScript ecosystem choose to name their configuration files with a suffix of rc. This naming convention can be seen in examples such as: ES Lint -> .eslintrc.json npm -> .npmrc.json yarn -> .yarnrc I am aware that the ...

Using a Google Cloud API that requires authentication without relying on a pre-existing client library

I'm facing some challenges while trying to deploy an application on GCP Marketplace due to the authentication documentation being unclear. According to the documentation, an OAuth scope of https://www.googleapis.com/auth/cloud-platform is necessary, w ...