• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

97
Views
Fetch a field value and compare it with values inside an array of sub documents in MongoDB

this is my mongoDB document, i want check if the date in sessionDate is present inside the array unavailableDates.

This is my schema

const adminSchema = new mongoose.Schema({
sessionDate:{
    type:Date
    
},
unavailableDates:{
   type: [Date]
},
 email:{
    type:String,
    required:true,
    min:6,
    max:255
},
password:{
    type:String,
    required:true,
    min:6,
    max:1024
},
});
 module.exports = mongoose.model('Admin',adminSchema);

This is my mongoose document

_id: objectId('6332d9846c4803d9ffc53bb0') 
"email":"someemail@mail.com"

 "password":"$2b$10$0ixZcynrlRhHQs14xJzPhO5Zn5scbWMrUFwbC/S6nrDI7qe9ZeJ0e"
unavailableDates: Array
0: 2022-09-08T18:30:00.000+00:00
1: 2022-10-09T18:30:00.000+00:00
2: 2021-12-31T18:30:00.000+00:00
3: 2022-01-01T18:30:00.000+00:00  
sessionDate: 2021-12-31T18:30:00.000+00:00
almost 3 years ago · Santiago Trujillo
1 answers
Answer question

0

If you want to check if at least one of unavailableDates array entries is same as sessionDate this is one option:

db.collection.aggregate([
{
 "$match": {
   "$expr": {
     "$in": [
        "$sessionDate",
        "$unavailableDates"
      ]
     }
   }
 }
])

playground

almost 3 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error