Exploring the structured hierarchy of subdocuments within nested arrays in MongoDB

I'm currently working on a query that needs to meet two criteria: matching a specific userID and also matching a range of IDs.

Here's an example of what a document might look like: https://i.sstatic.net/sIF3K.png

My approach involved using the find function to specify the userID, followed by utilizing MongoDB's $in operator to match a list of feature IDs.

  const featureIds = features.map(feature => feature._id)
    console.log(featureIds)
    const isFeatureExists = await this.userShopModel.find({
      userId,
      'items.feature': { $in: featureIds },
    })
    console.log(isFeatureExists)

I've also experimented with different versions of the aggregate method, but unfortunately haven't had any success yet.

If there are any MongoDB experts out there who could lend me a hand, I would greatly appreciate it!

Answer №1

Utilize _id for filtering

const featureIds = features.map(feature => feature._id)
    console.log(featureIds)
    const isFeaturePresent = await this.userShopModel.find({
      _id : userId,
      'items.feature': { $in: featureIds },
    })
    console.log(isFeaturePresent)

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

Stopping Angular Route Alteration Depending on Routing Conditions

I have been searching everywhere for a solution to this issue. My goal is to be able to access the routing parameters collection and prevent the route from loading if a certain parameter is missing. I have tried using preventDefault in $routeChangeStart, b ...

Differentiating categories in the second parameter for controller method in AngularJS?

As a newcomer to Angular, I have noticed that the locals argument in the controller function can sometimes be just a function and other times an array. angular.module('contentful').controller( 'FormWidgetsController', ['$s ...

VueJS: Incorporating a Computed Property within a v-for Loop

Is there a way to utilize computed properties in lists while working with VueJS v2.0.2? Check out the HTML snippet below: <div id="el"> <p v-for="item in items"> <span>{{fullName}}</span> </p> </div> A ...

Try to locate a particular sequence of characters

I am currently on a quest to locate a specific row within the database that corresponds to the message provided by the user, specifically: catalystname. After successfully indexing the given string as text within the schema: const { Schema } = mongoose; ...

The attempt to remove a cookie through a Next.js server-side operation was unsuccessful

Having trouble removing a cookie using the next/headers module in my Next.js application. The code snippet below is what I've tried: import {cookies} from "next/headers"; export default async function Signout() { async function deleteTok ...

Techniques for incorporating a variable into the value field in JavaScript

let y = data[1]; cell1.innerHTML ='<input id="text" type="text" value= "'y'"/>' ; This snippet of code does not render any content when attempting to pass the variable, but if you provide a specific value like "h", it will displa ...

The requested path /releases/add cannot be located

In my CRUD application, I have a feature that allows users to create releases by adding a version and description. This is achieved through a button and input fields for the details. <button (click)="addRelease(version.value, description.value)" [d ...

Using PHP to create multiple div elements and then concealing them all

I'm having an issue with the hide feature not working. My intention is to hide each dynamically generated div and gain control over them, but the problem seems to lie in the div id incrementing. Any assistance would be greatly appreciated! <?php $ ...

I am encountering the error 'user.matchPassword is not a function' while making a call to my API using bcryptjs in my Node.js and Express application. Can someone help me understand why

const checkUserAuth = asyncHandler( async (req,res)=>{ const { email , password } = req.body; const foundUser = User.findOne({email}); if(foundUser && (await foundUser.verifyPassword(password))){ generate ...

Finding the best way to transfer text between DIV elements?

I have a dilemma involving two DIV elements positioned absolutely on the sides of an HTML page, much like this EXAMPLE: <div class="left"> </div> <div class="right"> </div> These are styled using the following CSS: .left{ pos ...

Tips for setting up Nginx with Node.js on a Windows operating system

I am looking to set up Nginx on my Windows machine in order to run two node applications. Can anyone provide guidance on how to accomplish this? I have attempted to download Nginx 1.6.3, but have had trouble finding instructions specifically for running i ...

Using the native functionality, submitting a razor form with JQuery AJAX in MVC6

Is there a specific method to submit a form using jQuery AJAX in MVC6 while still utilizing the Auto Binding functionality of ASP.NET MVC? In previous versions of MVC, one could use jquery.unobtrusive-ajax and simply utilize @using (Ajax.BeginForm("SaveDa ...

Customize your Angular UI Bootstrap Datepicker with unique buttons - here's how!

Currently, I have a datepicker with clear and close buttons. I tried using the append function to add more buttons to this datepicker, but it didn't work because the content is not loaded until we click the icon since it's a popup datepicker. Is ...

Tips for limiting access to data from various tabs when the web page first loads

div#tabCtrl div#page1 This table showcases the information for tab1 div#page2 This table showcases the information for tab2 ...

Using a Do/While loop in combination with the setTimeout function to create an infinite loop

Having trouble with this script. The opacity transition works fine, but there seems to be an issue with the loop causing it to run indefinitely. My goal is to change the z-index once the opacity reaches 0. HTML <div class="ribbon_services_body" id="ri ...

Getting an Angular error while adding data to MongoDB with Unshift

I encountered an error when submitting data to MongoDB using Angular. The error is related to the use of the unshift keyword, but I am not very familiar with Angular. Can someone help me understand why this error is occurring? TypeError: Cannot read prope ...

Exploring Iconography in Amcharts

Is there a simple way to include images/icons within a chart using x and y coordinates? I'm new to javascript and amcharts (am4charts) so any assistance would be greatly appreciated! I've searched for solutions on stackoverflow, like this one o ...

Wrapping a method within a subclass of a Class

In our main class, the structure is as follows: from pymongo import MongoClient from credentials import MongoMarketPlaceAuth client = MongoClient('mongodb://conn_url:conn_port/', ssl=True) db_connection = client.DB_NAME db_connection.authentic ...

What is the best way to add a button to every row in Titanium Studio?

Is there a way to add a different button inside each row (createTableViewRow)? I have created five buttons using Titanium.UI.createButton, but I'm struggling to figure out how to place all five buttons in every row. Can someone provide some guidance o ...

Edit the contents within HTML strings without altering the HTML structure

If I have a string of HTML, it might look something like this... <h2>Header</h2><p>all the <span class="bright">content</span> here</p> I am interested in manipulating the string by reversing all the words. For example ...