How can we filter out each element from an array of objects in a MongoDB query based on given ad conditions?

I need to retrieve slugs for articles in my database based on their event dates.

"eventDate" : {
    "day" : "21",
    "month" : "04"
},
"slug": "some-slug"
...

The input I have is

[{day: '01', month: '02'}, {day: '02', month: '02'}]
and I want to find all articles that match this criteria. I attempted the following query:

.find({'eventDate': { $in: myArrayOfObjects}}, {slug: 1})

How can I correct this query to get the desired results?

Answer №1

Avoid using $in to search for objects. Instead, consider using $or in the following way:

db.collection.find({
  "$or": [
    {
      "eventDate.day": "01",
      "eventDate.month": "02"
    },
    {
      "eventDate.day": "02",
      "eventDate.month": "02"
    }
  ]
},
{
  "slug": 1
})

To test this method, you can visit: mongoplayground.net/p/olTwcwGuuFi

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

Turbolinks refusing to disregard page in Ruby on Rails application

Here is the front page view file ('Welcome/index.html.erb') : https://github.com/bfbachmann/Blog/blob/master/app/views/welcome/index.html.erb Current Rails Version: 4.2.6 The main issue at hand: I am currently struggling to display my THREE.js ...

What is the method for verifying a password in the login process when it has been hashed by bcrypt during registration?

Currently in the process of developing a signup and login page using Node.js with Pug, Mongoose, and bcrypt. I am encrypting and storing passwords in the database after registration or sign up. I'm facing an issue with the comparison function as it r ...

Looking for ways to locate an element using the Material-icons name?

Looking for the following HTML element using JavaScript: <i class="material-icons bh-icon-accordion">keyboard_arrow_up</i> I have multiple arrow_down elements and only one arrow_up element, so finding it by class is not an option. Since ther ...

How can a default option be shown at the top of a select dropdown list in AngularJS with dynamic content?

Objective: Arranging a default <option> to always be displayed at the top of a <select> dropdown upon opening. I am making progress towards my objective. We currently have a dropdown that dynamically populates based on selected elements, with ...

The content inside the bootstrap modal is not visible

My goal is to trigger a modal window when a specific div is clicked using bootstrap modal. However, upon clicking the div, the screen darkens but the modal body fails to appear. Code: <html> <head> <title></title> ...

Unable to bind knockout dropdownlist data during an ajax request

Trying to connect a dropdownlist in knockout with MVC 4. Below is the code snippet: Action public JsonResult GetUserTypes() { using (QuestApplicationEntities db = new QuestApplicationEntities()) { var usertypes = (from ...

A Guide to Effectively Extracting Data from Second Level JSON Arrays with

Hi, I'm having difficulty fetching the two attributes under CategoryImage in the second level of the JSON array parsing. Can someone help me with this? Thank you. <script> $(document).ready(function() { var cat=""; ...

Cannot find solutions for all parameters for [object Object] in Angular 2

Encountering an error when attempting to navigate to a component that utilizes the angular2-google-maps module. The specific error message is: Can't resolve all parameters for [object Object] (?). ; Zone: angular ; Task: Promise.then ; Unable to ...

What is the best way to retrieve a Promise from a store.dispatch within Redux-saga in order to wait for it to resolve before rendering in SSR?

I have been experimenting with React SSR using Redux and Redux-saga. While I have managed to get the Client Rendering to work, the server store does not seem to receive the data or wait for the data before rendering the HTML. server.js ...

GraphQL and Relay.js issue: Error Message: Field "id" is expected in "Node"

It appears that the discrepancy lies in naming conventions between my schema.js file and the database field. The 'id' field in the schema is actually named differently in the database. var classroomType = new GraphQLObjectType({ name: 'Cl ...

Using Star ratings in amp CSS with Django template Tag: A complete guide

I need to implement a star rating display on amp pages based on values retrieved from the database using django template tags. How can I achieve this considering the ratings are in float format? Additionally, is it possible to apply the same code to non-am ...

Guide on extracting value from XML data using $.ajax and then displaying it within a $.each loop

As part of our study project, we have a task that involves comparing an array of strings with an XML database. My approach was to break it down into two parts since we need to perform the comparison function twice. First, I iterate through the array using ...

Is it advisable to incorporate await within Promise.all?

Currently, I am developing express middleware to conduct two asynchronous calls to the database in order to verify whether a username or email is already being used. The functions return promises without a catch block as I aim to keep the database logic se ...

Switch the accordion to open or close

I am struggling to create an accordion menu that toggles open and close using the same button. Right now, it only opens and I cannot figure out how to make it close as well. Below is my current code, or you can view it on jsFiddle: http://jsfiddle.net/nd ...

What is causing the React text component to fail to show changes made to my array state?

I am currently in the process of learning React Native. I have been working on an app and encountered an issue while attempting to update individual elements within an array. Here is an example of the code: function MyApp() { const [array, setArray] = ...

Struggling to understand Regular Expressions in JavaScript

Looking to implement validation regex on a specific text field, here's an example input: 1364-lqap-10926 This requires a format of 4 digits, dash, 4 letters, dash, and 5 digits (total length must be 15 characters). I attempted using \d{4})-([a ...

What is the best way to ensure a DOM element exists before you can start manipulating it?

In my AngularJS application, I am utilizing Angular Material and md-select. When a user clicks on the md-select, a dropdown list appears and an overlay with the tag Name is added to the DOM. My goal is to add a class or style as soon as the user clicks on ...

JavaScript form validation eliminates input data

I recently started using the JavaScript library for client-side form validation called Bouncer. It has been working really well, but I have encountered a peculiar behavior that I need help understanding. My form has two submit buttons with different value ...

Structuring an E-commerce Database with MongoDB

I am in the process of developing a multi-user ecommerce application geared towards a specific industry, similar to Shopify. At the moment, I am utilizing Node.js with MongoDB as my backend technology. One question that has come up is what would be the bes ...

How to reference a variable using a string in AngularJS

I am trying to update the state of a variable by accessing it. function changeValue(source, target, unit) { $scope[target] = process($scope[source] + unit + ".").toKilograms(); }); This function is triggered every time something is inputted i ...