Searching for a string in an array using the $in operator in M

I have a custom model defined as follows:

var MessageSchema = new Schema({
    msgID: String,
    patientList: [String],
    created_at: Date,
    updated_at: Date
});

The patientList is always dynamic in nature.

I am attempting to construct a Mongoose query that can retrieve all records where a specific patientID is present within the patientList of a given message. However, my current implementation returns empty results.

    var patientID   = req.params.patientID;
    var query = Message.find({});

    if(patientID){
        console.log(patientID);
        query = query.where(patientID).in('patientList');
    }

All the existing queries I've come across are aimed at searching within an array with fixed values. In my scenario, the patientList may contain any number of patientIDs.

Any suggestions or solutions would be greatly appreciated.

At present, the output generated is:

{
"name": "MongoError"
}

Answer №1

Oh, I finally discovered the solution! It turns out that this code snippet does the trick:

query = query.where('patientList').equals(patientID);

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

There was a void in the supertest request with the application/vnd content type

I'm struggling to send a request body using supertest for a post request. Despite configuring body-parser as suggested in other answers, the issue persists. I've checked solutions that mention misconfiguration of body-parser, but they seem to be ...

Challenges with setting up Vue.js

I've been attempting to install vue.js but have had no success, even though my npm is up to date. When I try running vue init webpack vueapp, I encounter the following error: No command 'vue' found, did you mean:Command 'vpe' fr ...

Issue encountered with Ionic and ssh2: process.binding is not supported

I am currently delving into the world of Ionic and experimenting with creating a basic application that utilizes SSH2 to establish an ssh connection between the app and a server. Here is a breakdown of the steps I took to encounter the issue: Steps to Rep ...

Tips for managing an interval for play, pause, and stop functions in JavaScript or Node.js

In my main file, I have an API to control the playback of a video. main.js const { mainModule } = require('process'); const { startVideo, pauseVideo, stopVideo } = require('./modules/video.js'); function init(payload) { if(payl ...

Is there a way to determine if npm packages are accessing and misusing my system's environment variables?

Apologies if this seems nonsensical. But including a code snippet like this: // to illustrate I'm utilizing a source from https://www.npmjs.com/package/got got.post(maliciousUrl, {json: process.env}) Is it enough to leak environment variables to an u ...

What is the process for transforming an asynchronous method into a synchronous version?

I am currently working on creating a functionality similar to the core fs module's methods, where there is an Async method by default and a Sync method if requested like fs.readDir() and fs.readDirSync(). In my case, I have a method named fetchUrls w ...

Having trouble deploying Heroku with Node.js due to a "module not found" error?

I've hit a wall with my problem. Despite trying everything, I still can't successfully deploy my nodejs server on Heroku. It's puzzling because running the start script using the heroku run bash command works perfectly fine. However, when I ...

Empty array in req.body using Express, Multer, and BodyParser would result in no

Apologies for my lack of experience, but I am encountering an issue with a form that uploads images and adds text to the database. The images upload successfully, however, the req.body object is consistently returning as an empty array. HTML <form cla ...

Encountering the following error message: "MongoServerError - E11000 duplicate key error on collection: test.users, index: email_1. Duplicate key found: {email: null}"

I'm a student working on creating a login authentication API using Node, Express, and MongoDB. My issue is that I can't add multiple users when signing up. Despite searching through numerous threads on this website, I haven't found a solutio ...

Leverage NPM to download and set up a codebase for your development needs

When using npm install foo, the result is: foo being installed to ./node_modules/foo No version control repository. However, it would be more convenient if NPM could detect the version control repository location and allow us to execute npm start-dev fo ...

Using Node.js with sqlite3 for seamless data synchronization

Recently, I've been experimenting with developing a basic app using nodejs, socket.io, and sqlite as the database system. This project is primarily for learning purposes to gain a better understanding of how nodejs functions and to determine its suita ...

lotion.js is throwing an error in the program that keeps track of the total number of transactions that have taken place (TypeError: app.listen is not recognized as

let app = require('lotion')({ initialState: { count: 0 } }) app.use((state, tx) => { state.count++ }) app.listen(3000) I encountered an issue when trying to run the code above from the official lotion js website that resulted in the fol ...

Guide to incorporating external proto definitions into NodeJS using proto-loader

The URL for making RPCs is flow-testnet.g.alchemy.com:443 I am currently missing all .proto files on my local system. What is the process for loading them in order to create package definitions? ...

Error encountered: "Missing fs module in Expo and NextJS build"

After setting up a monorepo for a NextJS + Expo React Native cross-platform application using Solito (), I successfully deployed both web and iOS apps. However, I encountered a build error in the Expo iOS app after accidentally wiping my local repository a ...

Having difficulty with pagination within a callback function

I have been attempting to paginate in a call to a callback function, but I am encountering an error on the second call. Here is what my function does: let content = '' let size = 100 let from = 1 function result(size, from, callback) { ap ...

Searching with a reference in Firestore

I have been struggling to locate all documents with a specific reference field in a collection within Firestore. While I have explored various articles on this topic, none of the solutions seem to be effective for me. I am hoping someone could pinpoint whe ...

Using the Mongoose $or operator with a nested array in query conditions

Here are the schemas I am using: //ProjectModel const ProjectSchema: Schema = new Schema( owner: { type: Schema.Types.ObjectId, ref: "User" }, users: [{type: Schema.Types.ObjectId, ref: "ProjectUser", unique: true }] ); //Project Use ...

The Node Js Version is currently unavailable on the shared server

As I work on developing a website with Node Js app, tailwind css, and Next.Js, I have encountered an issue. The app functions correctly locally on my computer using Git Bash with Node.js version 18.17.0 and higher. However, the hosting server running cPane ...

Using Webpack alongside Next.js may result in improper bundling of files in the client bundle

Currently, my Next.js application utilizes mongoose to establish a connection with my mongodb. The models import db.ts in order to ensure an active connection to the database like this: import { model, models, Schema } from "mongoose"; import &qu ...

The installation of NPM packages is failing when attempted with npx

I've encountered an issue while working on a CLI application using NPM. After packaging the application and trying to run it, I'm getting the following error: > npx -y [organization]-[package]-0.0.1.tgz node:internal/errors:496 ErrorCaptur ...