Exploring particular sub documents within MongoDB utilizing Mongoose

I have tested multiple methods from the MongoDB documentation manually, but none of them seem to resolve my issue. Below is a structure of my data for reference:

"loopback": [
        {
            "_date": "some date",
            "dutTestParams": [],
            "_id": "5f680665dfbfb74e78cae175",
            "paramId": "dummyid",
            "jtagApbStatus": "true",
            "dutYaml": "dummy_yaml",
            "dutSequenceId": "dummy_sequence",
            "dutGitVersion": "1.0.1",
            "guiVersion": "1.0",
            "projectId": "alphacore",
            "jobId": "id",
            "pvt": "dummypvt",
            "rate": 1,
            ...

My main document contains test data related to the test points and all chips tested within the test.

While filtering the queried data is straightforward, I am seeking advice on making specific queries. Ideally, I want to execute a query like this:

db.testdata.find({jobid:"some id", data.0.chipid:"chip1"})

The goal here is to retrieve the parent header document along with only sub-documents where chipid equals "chip1". However, my attempts fetch all sub-documents instead of just those that match the specified chipid.

If anyone has experience in resolving this issue, your help would be greatly appreciated!

Answer №1

Check out this query -

db.collection.aggregate([
  {
    "$match": {
      employeeId: "12345"
    }
  },
  {
    "$unwind": "$entries"
  },
  {
    "$match": {
      "entries.status": "active"
    }
  }
])

Test it on Mongo Playground

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 strategies can I use to reduce the amount of event listeners assigned to buttons in jquery?

Currently, I am utilizing jquery 1.6.2 for my project. On one of the pages, I have a structure that resembles the following: <div id="section1"> <fieldset> <ul> <li> <input type="radio" name ...

Error message on Android Web Console: ReferenceError - Worker object is not defined

As someone who is new to javascript, I have been struggling to understand why popular browsers seem to handle the definition "new Worker("BarcodeWorker.js")" differently than Android WebView. The original code for this Barcode Reader is from Eddie Larsso ...

Fetching information from WebMethod with Jquery Ajax in c#

I have a jQuery variable that contains the following values: var data = [['Vikas', 75], ['Sumit', 55], ['Rakesh', 96], ['Shivam', 123], ['Kapil', 34], ['Rana', 104]]; Now, according to my requir ...

The select box in Material UI is not displaying the data as expected

I'm currently tackling an issue where, upon every click of an icon, a select box (from material ui) needs to be displayed with a few options inside it. The functionality should show the select box each time the icon is clicked. Here's a brief sum ...

I am looking to connect this piece of javascript to a button on the webpage

<script> var data; function getData() { data = prompt("What year would you like the data for?"); showData(data); } function showData(data) { if (data == 1901) { alert("26 thousand per million for males and 23 for females "); ...

How to handle a Node.js promise that times out if execution is not finished within a specified timeframe

return await new Promise(function (resolve, reject) { //some work goes here resolve(true) }); Using Delayed Timeout return await new Promise(function (resolve, reject) { //some work goes here setTimeout(function() { resolve(true); }, 5000); } ...

Error with the login component in React.js (codesandbox link included)

Hey there! I'm a beginner programmer and I recently dove into a tutorial on creating a Login Form. However, I've run into a few issues along the way. Overview of My Project: The login form I'm working on was built using create-react-app. T ...

Discovering distinct values for every column in a Mongodb query set with the use of Node.js

Currently, I am utilizing nodejs to retrieve data from a mongodb collection. However, while I successfully fetched all the data from the collection, I would prefer the outcome to be structured as outlined in the Expected Result. The current result mirrors ...

Exploring the effectiveness of React Hook Form using React Testing Library

My Component includes a form that, upon submission, utilizes Apollo's useLazyQuery to fetch data based on the form values. The form in the component is managed by React Hook Forms, with the handleSubmit controlled by RHF. <FormContainer onSubmit= ...

Experiencing issues with the Google Drive file selection API due to JavaScript

I am having trouble implementing a Google file picker in my Django Project. Every time I try to create an HTML page with an integrated Google file picker, I encounter a 500 "Something went wrong" error. https://i.sstatic.net/6JMh7.png This issue arises a ...

Capturing errors during function declaration in JavaScript

A problem has arisen in the below function definition due to a script error function foo () { try { var bar = function() { ERROR } } catch (exception) { console.debug("exception"); } } foo(); However, th ...

"Tips for stopping users from using Ctrl+U to view page source in

Is there a way to prevent users from viewing the source code (HTML + JavaScript) of a page by disabling Ctrl+U in the browser? ...

Explore the AngularJS ui-routing project by visiting the website: https://angular-ui.github.io/ui-router/#resources

I am currently working on creating a sample app using AngularJS ui-routing. There is a tutorial that I am following which can be found here When I try to run the site locally in Chrome, I am encountering some errors in the console. Below are the errors tha ...

There seems to be a problem with how the navbar is being displayed in ResponsiveSlides.js

I am currently using WordPress, but I have come here seeking help with a jQuery/CSS issue. I am utilizing responsiveSlides.js to create a simple slideshow, however, when viewing it here at gallery link, it appears that something is not quite right. STEPS: ...

Express Node fails to launch

I recently decided to try my hand at Express and learn more about it. However, I encountered an issue while trying to start a server. Initially, I attempted to access 127.0.0.1:8080 through Express but nothing seemed to be happening. Although I copied most ...

Encountering CORS Error: Challenge in sending post requests with NodeJs and Angular

Whenever I attempt to make a post request, I encounter the following error message: Access to XMLHttpRequest at 'http://localhost:3002/api/products/checkout' from origin 'http://localhost:4200' has been blocked by CORS policy: Request ...

What is an alternative way to retrieve the page title when the document.title is unavailable?

Is there a way to retrieve the page title when document.title is not available? The code below seems to be the alternative: <title ng-bind="page.title()" class="ng-binding"></title> How can I use JavaScript to obtain the page title? ...

"Setting a maximum height for a div element, along with nested divs and paragraphs

.wrap { width:400px; height:200px; border:1px solid #000; } .content { width:300px ...

What are the steps to store a firebase storage downloadURL in a firestore collection?

I'm facing an issue with saving my firebase storage image into a firestore collection variable. Initially, it was working correctly but suddenly stopped functioning, and now the image variable is returning null. Note: I am using the asia-south1 serve ...

Is it necessary to use callbacks when using mongoose's findbyid with express to retrieve an object from the database? Are callbacks still important in modern JavaScript frameworks?

I'm currently exploring the express local library tutorial on MDN docs and wanted to try out returning an object without relying on a callback method. When I provide the request object parameter for the id to the findById mongoose method like this va ...