Harnessing $in Variables

I defined a request variable, but I can't seem to use it in the $in block:

{
  $lookup: {
    from: 'users',
    as: 'user',
    let: {
      "blogIds": "$blog.id"
    },
    pipeline: [{
        $project: {
          id: 1,
          user_name: 1,
          picture: 1,
          blogs: 1
        },
      },
      {
        $match: {
          blogs: {
            $in: ["$$blogIds"]
          }
        }
      },
    ]
  }
}

What could be causing this issue?

If I try sending without $match, but with '$addFields':

{
  $lookup: {
    from: 'users',
    as: 'user',
    let: {
      "blogIds": "$blog.id"
    },
    pipeline: [{
        $project: {
          id: 1,
          user_name: 1,
          picture: 1,
          blogs: 1
        },
      },
      {
        $addFields: {
          field: "$$blogIds"
        }
      },
    ]
  }
}

https://i.sstatic.net/zMK8U.png

An example of the document:

https://i.sstatic.net/yOlEW.png

Lorem ipsum dolor amen.

Answer №1

Do not attempt to input internal fields into another internal field,

Instead, use the expression $expr operator to address this issue. The $expr can construct query expressions that compare fields within the same document in a $match stage.

{ $match: { $expr: { $in: ["$$blogIds", "$blogs"] } } }

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

Quick method to populate an array with elements

I need to populate an array with a large number of objects. Currently, my approach looks like this: let useVertices = []; const len = this.indices.length; for(let i = 0; i < len; i++){ let index = this.indices[i]*3; useVertices.push(new THREE.Ve ...

Retrieving user data using axios in React to fetch user_id information

I'm currently working on a React project where I have created a simple list and I am fetching data using axios. Here's a snippet of my code: https://codesandbox.io/s/old-worker-6k4s6 import React, { useState, useEffect } from "react"; ...

Description of the image when clicking the next or previous button

Hey there, I'm new here and I could really use some help with an issue I'm facing regarding the image description in my gallery. When I click on a thumbnail, the description shows up just fine, but when I try to navigate using the next and previo ...

Transfering information to handlebars in node.js

Being a beginner in node.js, I am currently working on making a get request in my router (index.js). After successfully obtaining the desired result (verified by logging it in console.log), I proceed to parse it into a JSON object and pass it to a render f ...

Using jQuery to iterate through JSON data obtained from a web service

In this code snippet, I am attempting to retrieve a JSON response from a PHP page and then iterate through it to display the name field of each JSON object. However, for some reason, nothing is being alerted out. <html> <head> <title>A ...

Avoid calling the inherited method multiple times in ES6 classes

Transitioning from RequireJS to browserify (along with babelify) has led me to rewrite my current modules as classes. Each of my RequireJS modules has a method named eventHandler that manages module-specific events. However, when extending a class, the sub ...

Passing variables to Express routes: a guide

I'm in the process of creating a website where I require an ID to be transmitted to an express route. The button with the ID 'approve_appln' triggers a route. Below is the associated JavaScript: var appr_appln = document.getElementById(&apo ...

Once the "approve" button on a sales order is clicked, my intention is to generate a purchase order

When a user clicks the approve button on a sales order, I need to generate a purchase order. I have deployed a script to the sales order records with the event type set to trigger when the approve button is clicked. However, I am encountering an issue wher ...

Issues arising from Ionic's functionalities when separated controllers are implemented, resulting in

I utilized the yeoman generator to create an Ionic app. After starting the app with grunt serve, I added a new controller called settings. Index.html: <script src="scripts/controllers/settings.js"></script> Settings js: 'use strict&apo ...

Establishing pathways in Angular 2

I'm currently working on configuring a route so that whenever I visit http://localhost:8080/user/beta/, it will redirect to http://localhost:8080/user/beta/#spreadsheet. The goal is to display the spreadsheet view, but instead of achieving that result ...

Having trouble with Postgres not establishing a connection with Heroku

My website is hosted on Heroku, but I keep encountering the same error message: 2018-05-06T19:28:52.212104+00:00 app[web.1]:AssertionError [ERR_ASSERTION]: false == true 2018-05-06T19:28:52.212106+00:00 app[web.1]:at Object.exports.connect (_tls_wrap.js:1 ...

How can data be transferred from a parent component to a child component using MaterialUI's styling functionality?

Recently delving into the world of reactjs, I've been tinkering with a basic page that incorporates authentication using state. To spruce up the design, I decided to use the MaterialUI framework. The issue I'm facing is related to sending the lo ...

Personalized Data Structure in Mongoose

As someone who is new to MongoDB, I am currently working on a project that involves creating a professional network. My challenge lies in differentiating between ObjectIds of the same field within a Schema. Let's consider the following example: Schem ...

Putting Text Inside a Video Player in HTML

Is there a way to insert text, like a Logo, into my video player? https://i.sstatic.net/CZ6Rp.png I would appreciate any help on how to achieve this. Thank you. <video width="320" height="240" controls src="video/flashtrailer.mp4"> Your browser ...

Learn how to call class methods using jQuery events by utilizing the on() method

I'm attempting to create a reusable "component" using both HTML5 and accompanying JavaScript code that can be utilized multiple times. First, let's examine the HTML5 component code: <div class="listEditor" id="myStringList"> <div id="li ...

"Waiting for results with Node.js and Mongo DB is like trying to catch a

I am currently working on developing a Web Api using NodeJs and MongoDB. One issue I have encountered is that my await statements are not being awaited, leading to unexpected behavior in my code. Code async find_nearby_places(lng, lat, tag, maxDistanc ...

How can PHP effectively interpret JSON strings when sent to it?

When working with JavaScript, I am using JSON.stringify which generates the following string: {"grid":[{"section":[{"id":"wid-id-1-1"},{"id":"wid-id-1-4"}]},{"section":[{"id":"wid-id-1-5"}]},{"section":[{"id":"wid-id-1-2"},{"id":"wid-id-1-3"}]}]} I am ma ...

Troubles with configuring the Express server in relation to the public directory

After creating two separate bundles for my server and client, I encountered an issue where the client bundle is not being downloaded by the browser when accessing the root route. To address this, I instructed Express to treat the public/ folder as a freel ...

How can I activate the copy function in jQuery?

I'm trying to figure out how to initiate a copy event using JavaScript or jQuery. I need to be able to simulate the copy event by clicking on a button, but I haven't been able to find a solution yet. I want to avoid using ZeroClipboard or any oth ...

What is the best way to dismiss the additional modal popup?

Here is an example that I need help with: http://jsfiddle.net/zidski/Mz9QU/1/ When clicking on Link2 followed by Link1, I would like the Link2 box to close automatically. Is there anyone who can assist me with this issue? ...