Using multiple $lookup with arrays of objects: A guide

Suppose I have a collection with the following data structures:

post col:

{
    _id: ObjectId("5fce0e137ff7401634bad2ac")
    address: "new york"
    description: "this is a great city"
    image: "images\0.4644859674390589-gikiguk.jpg"
    userId: "5fcdcd8487e37216bc82171b"
}

user col:

{
    _id: ObjectId("5fcdcd8487e37216bc82171b")
    name: "jack"
    profile: {image: "images\0.4644859674390589-dofowfg.jpg", description: "be happy dude"}
    email: "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="196d7c6a6d596d7c6a6d377a7674">[email protected]</a>"
    password: "2a$12$gzRrY83QAjwLhdyvn3JQ2OcMj3XG65.BULva4cZqcuSxDhhMbSXCq"
}

likesComments col:

{
    _id: ObjectId("5fce0e191ff7301666kao2xc")
    likes: {quantity: 1, likes: [{isActive: true, userId: "5fcdcd8487c31216cc87886r"}]}
    comments: {quantity: 1, comments: [{userId: "5fcdcd8487c31216cc87886r" , comment: "awesome city"}]}
    postId: "5fce0e137ff7401634bad2ac"
}

The scenario involves searching through all posts belonging to a specific user by their userId, retrieving post data from the posts collection, performing a $lookup operation on the users collection to gather user information, and then using postId to look up associated data in the likesComments documents. However, when a post has no likes or comments, the queries return an empty array, but they function properly if there are likes and comments present.

The desired output should resemble the structure below:

    [
      {
        postId: '5fce0e137ff7401634bad2ac',    
        location: 'shiraz', 
        description: 'this is a greate city',    
        image: 'images\\0.4644859674390589-gikiguk.jpg',   
        userId: '5fcdcd8487e37216bc82171b',   
        name: 'mohammad',   
        profile: 'images\\0.6093033055735912-DSC_0002_2.JPG',    
        comments: { quantity: 1, comments: [{userId: "...", name: "...", profile: "...", comment: "..."}] },   
        likes: {quantity: 1, [{userId: "...", name: "...", profile: "..."}]}   
      }
    ]

query:

I am aware that my query contains errors, but I am unable to identify them at the moment.

db.collection("posts")
          .aggregate([
            { $match: { userId: { $in: users.map(u => u) } } },
            {
              $project: {
                _id: 0,
                userId: { $toObjectId: "$userId" },
                postId: { $toString: "$_id" },
                location: "$address",
                description: "$description",
                image: "$image"
              }
            },
            ...
          ])
          .toArray();

Answer №1

You've got a few issues to address in your example:

  1. Make sure to double-check the collection names when using $lookup. There are inconsistencies with some of your collection names.
  2. Pay attention to the data types (e.g., string vs. ObjectId) for the fields you're trying to match in $lookup. Mismatched data types can lead to unsuccessful lookups. Consider using sub queries to handle type conversions before performing the lookup.
  3. Some of the ObjectIds you're working with contain non-hex characters, like in the _id field of the likeComments collection.
  4. There seems to be a discrepancy in the data itself; certain ids do not align properly, such as likeComments.likes.likes.userId compared to user._id.

To assist you, here is the modified FIRST HALF of your pipeline that addresses some of the above points. You can refer to this as a guideline to help resolve the remaining issues.

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

Flipping a binary array?

Is there a optimized method for inverting a binary array of any length? For example, changing all 1s to 0s and all 0s to 1s within the array. var arr1 = [ 0, 0, 1, 1, 1, 0, ..., 1 ]; ...

divide the information within an HTML table cell

One way I have figured out to populate the cell of a table is by using the following code: {% for row in data %} <td> {{ row[4] }}</td> Now, let's say I have the data "['sugar']:['3642847']:['2020-08-21' ...

Image can be centered, but unable to center div in IE7

When trying to center an element both vertically and horizontally, everything seems to be working correctly except for one issue I'm facing in IE7. I am able to center an img vertically but not a div. What style is being applied by IE to the image tha ...

Creating a variable name from a string using a function

I am creating a log to keep track of the functions that are called. One specific function I am using is record_activity( function_name ); I find it tedious to manually add this at the beginning of every function I want to monitor. Currently, my functions ...

What is the solution to fixing the JSON parsing error that says 'JSON.parse: bad control character in string literal'?

When sending data from NodeJS Backend to the client, I utilize the following code: res.end(filex.replace("<userdata>", JSON.stringify({name:user.name, uid:user._id, profile:user.profile}) )) //No errors occur here and the object is successfully stri ...

Header fixed vertically

I am currently working on a webpage with a minimum width requirement of 1124px. This means that whenever the browser window is smaller than that, the content of the page needs to be scrolled horizontally instead of smoothly transitioning into a responsive ...

What is the best way to utilize regex to replace two elements simultaneously?

I am facing a challenge with manipulating a string of characters by adding span tags to highlight specific words and change their color. While I have successfully implemented the changes for one pattern using regex, I'm struggling to do the same for a ...

data retrieval error using promise

I am struggling to figure out how to use promises effectively. I'm facing an issue with promises while trying to return a new state in the loadPosts function after fetching data from an API: [loadPosts]: (state, index) => { fetchPosts().then(d ...

A guide to integrating ffmpeg with NuxtJS

I am completely new to Nuxt and currently in the process of migrating a Vue application that generates gifs using ffmpeg.wasm over to Nuxt.js. However, every time I try to access the page, the server crashes with the following error message: [fferr] reques ...

Subcomponent not receiving property correctly passed

I am currently working on a website that fetches property-related data from SDFI, a Danish national data provider. After incorporating feedback and restructuring the props component based on your suggestions, I encountered an issue with passing accurate va ...

How can I effectively monitor entry and exit events in polygons for multiple locations using mongoDB?

In my current project, I am dealing with a large number of vehicles (approximately 2000) and multiple polygons stored in my database using MongoDB. These polygons represent various entities such as cities, countries, intersections, etc., totaling 4 differ ...

Creating dynamic scroll animations for sidebar navigation in a single-page website with anchor links

I need help creating a seamless transition between anchor points on a single page, while keeping a fixed navigation menu that highlights the active section. As a novice, I am unsure how to incorporate "( document.body ).animate" or any other necessary code ...

Error: The function $(...) does not contain a progressbar

I encountered a TypeError saying $(...).progressbar is not a function when loading the Gentelella - Bootstrap Admin Template Page. This error is affecting many jQuery processes. How can I resolve this? Error in my Code: (custom.js) // Progressbar if ($( ...

default browser's drag and reposition feature

I'm trying to make an image draggable on my webpage by using default browser behavior, but it's not working for some reason. Here is the code snippet I'm using: <div style="position:relative;width:1000px; height:900px;border:solid;z-inde ...

The layout in Nextjs _app does not stay constant; it refreshes with each page change

After creating a _app.js file: const MyApp = ({Component, pageProps}) => { const getLayout = Component.getLayout || ((page) => <Layout>{page}</Layout>) return getLayout(<Component {...pageProps} />) } export default ...

Can someone guide me on how to extract checkbox values in a post method using Angular

I'm facing an issue with a table that contains a list of rules. Whenever the checkboxes are clicked, I want them to send a "true" value to an API endpoint. However, I keep receiving an error stating that the "associated_rule" is undefined. After tryi ...

Applying CSS rules from an array to elements by looping through

I'm looking for a way to allow users to input CSS styles and have those styles applied to the last selected element, which is determined by the "rangeselector" variable. Currently, the code selects the correct element, but only the first CSS rule is b ...

Observing and showing profound modifications in Vue

I need to show a distinct message for each category that the user selects <v-select multiple style="position: relative; top: 20px;" color="white" v-if="count == 3 && question" solo placeholder="Please Cho ...

Count of jSon objects

After receiving a string st from a web service, I convert it into an object. How can I determine the number of arrays inside this object? In this particular case, there are 2 arrays. var st = "{[{"Name": "fake", "Address": "add"]},[{"Name": "fake", "Addre ...

What is the best way to align an element next to another using id or class?

I am looking to align the search element next to either "Media Heading 1" or "Media Heading 2" based on their id/class. For instance: Assume I have an element with the class of "media-item-1" and I aim to position the search div alongside that element us ...