Querying GraphQL: Retrieving partial string matches

I have set up a connection to a mongoDB collection using graphQL. Here is the data from the DB:

{
    "_id" : ObjectId("59ee1be762494b1df1dfe30c"),
    "itemId" : 1,
    "item" : "texture",
    "__v" : 0
}
{
    "_id" : ObjectId("59ee1bee62494b1df1dfe30d"),
    "itemId" : 1,
    "item" : "pictures",
    "__v" : 0
}

Running the query

{ todo(item: "texture"){ itemId, item } }
returns:

{
  "data": {
    "todo": [
      {
        "itemId": 1,
        "item": "texture"
      }
    ]
  }
}

I am looking to find datasets that partially match a given string. For example, the string tur should return both datasets: texture, pictures

My graphQL Schema is defined as follows:

var schema = new GraphQLSchema({
  query: new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
      todo: {
        type: new GraphQLList(todoType),
        args: {
          item: {
            name: 'item',
            type: new GraphQLNonNull(GraphQLString)
          }
        },
        resolve: (root, {item}, source, fieldASTs) => {
          var projections = getProjection(fieldASTs)
          var foundItems = new Promise((resolve, reject) => {
            ToDoMongo.find({item}, projections, (err, todos) => {
              err ? reject(err) : resolve(todos)
            })
          })
          return foundItems
        }
      }
    }
  })
})

Answer №1

One common approach is to build the query for MongoDB in the following manner:

{ "item": /tur/ }

Using this concise syntax with the $regex operator, you can create the query within your ToDoMongo.find method.

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

What is the best way to prevent an HTML form from being submitted when a user is not logged in, but still allow submission when the user is signed

One of the requirements for users of my application is to be signed in before they can submit form information. Upon clicking on the submit button, my jQuery script verifies if the user is already signed in. If the user is not signed in, an error message ...

The Material UI slider vanishes the moment I drag it towards the initial element

After moving the Material UI Slider to the initial position, it suddenly vanishes. via GIPHY I've spent 5 hours attempting to locate the source of the issue but have not had any success. ...

Tap the <li> element in a select2 dropdown from Bootstrap using internjs

I am currently encountering an issue while trying to select values from a Bootstrap-select2 drop-down in my form during the process of writing some function tests. If Select2 is unfamiliar to you, here are some examples that may help: The drop-downs are ...

The ng-message function appears to be malfunctioning

I am facing an issue with the angularjs ng-message not working in my code snippet. You can view the code on JSfiddle <div ng-app="app" ng-controller="myctrl"> <form name="myform" novalidate> error: {{myform.definition.$error ...

Tips for incorporating a value within the AngularJS select function

Having an issue with passing a list value in the html select method using AngularJS. Here is my code: app.js $scope.subcategory = function() { var query = "SELECT unit FROM Length;"; $cordovaSQLite.execute(db, query).then(function(res) { ...

Executing shell scripts on the backend of a Node.js web application: A step-by-step guide

Currently, I have a full stack MERN application up and running on a Google Compute Engine, with a local MongoDB also running on the same server. This application is a CRUD system. However, I have a specific script stored on the server that I want to be exe ...

Exploring Node.js promises using mongoskin

Currently, I'm seeking alternatives to using callbacks for my mongodb queries. I've been utilizing mongoskin for making calls like this: req.db.collection('users').find().toArray(function (err, doc) { res.json(doc); }); As I often n ...

Cube area to be filled

I am struggling to fill a cube with colors as it is only getting half of the figure filled. I suspect there might be an issue with the cubeIndices, but I'm having trouble figuring out how to make it fill everything. While I know I could use a cylinder ...

Running Javascript code after rendering a HandleBars template in Node/Express

I have a Node.js application where I am trying to load data into a jQuery datatable after the data has been fetched. Currently, I am able to populate the table with the data, but I am facing issues initializing the datatable. Here is how I render the temp ...

Code is not running in ReactJS

My challenge is to use canvas and script to draw a rectangle with one diagonal line. However, when I try to do so, only the rectangle appears on my browser. It seems like the script is not being executed. Here's the code: import React, { Component } ...

Grails domain criteria triggered a MongoDB CursorNotFound exception for an active cursor

Currently, I am utilizing Grails 2.4.4 along with mongo plugin 3.0.2 and MongoDB 2.4.10 to establish a connection with a remote database. grails { mongo { host = "11.12.13.14" // Remote server IP address port = 27017 databas ...

Tips for defining a distinct series of key-value pairs in typescript

Having experience with a different language where this was simple, I am finding it challenging to articulate a sequence of pairs: Each pair is made up of two basic elements (such as strings or numbers) Each element may appear multiple times within the lis ...

What is the proper procedure for entering data in the correct sequence using JavaScript?

I am currently utilizing Node.js to send data to a SQL Server table. Within the code, there is a for loop that calls a function triggering an insert statement (which will eventually transition to being a stored procedure). This loop iterates through variou ...

Utilizing Conditional Statements in the @artsy/fresnel Framework

I recently started working on a responsive React application using the @artsy/fresnel npm package. Below is a snippet of the code I have implemented: <Media greaterThanOrEqual='computer'> <div style={{ padding: '20px 50px' ...

Selecting from a variety of options presented as an array of objects

I am currently working on a component that allows users to select roles: https://i.stack.imgur.com/bnb9Y.png export const MultipleSelectChip = ({ options, label, error, onRolesUpdate, }: Props) => { const theme = useTheme(); const [selected ...

Adding a player object to a Phaser scene using JavaScript

Just starting out with Phaser js and attempting to create a game using Object-Oriented Programming. The challenge I'm facing is trying to load my character into the scene, but nothing seems to be happening. No errors or exceptions are being thrown. I& ...

Tips for extracting only a portion of the JavaScript timestamp

I have a JavaScript timestamp that reads Tue Sep 30 2014 12:02:50 GMT-0400 (EDT). When I use the .getTime() method, I get 1412092970.768 Typically, this timestamp represents a specific time of today. My question is whether it's possible to extract o ...

When the user clicks on a specific element, ensure that it is the main focus and generate an overlay

One of my challenges is implementing a custom element that captures user input upon clicking, focusing on it and overlaying other elements. I want the overlay to disappear if the user clicks outside the div. I attempted to achieve this using the iron-over ...

The behavior of Elementor lightbox buttons upon being clicked

When using Android, I've noticed that the lightbox briefly displays a semitransparent cyan bar on the left and right buttons when they are pressed. Is there a way to control or prevent this behavior? Any suggestions would be appreciated! Thanks in adv ...

Determining the file path in HTML5

Can anyone help me with retrieving the file path using html5 javascript once a user selects a file? I require the file path for a specific scenario: In this case, the user uploads a file and pauses it (currently only supported by Mozilla browsers), then c ...