Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
finding object from array of objects in Mongodb
const Schema = new Schema({
    queueId: {
        type: String,
        required: true,
        index: {
            unique: true
        }
    },
    players: {
        type: [
            {
                ID: {
                    type: String,
                    required: true,
                    index: {
                        unique: true
                    },
                    default: 'null'
                },
                name: {
                    type: String,
                    required: true,
                    default: 'null',
                },
                queueId: {
                    type: String,
                    required: true,
                    default: 'null'
                }
            }
        ],
        required: true,
        default: []
    },
    isAvailable: {
        type: Boolean,
        required: true,
        default: true
    },
    isFull: {
        type: Boolean,
        required: true,
        default: false
    }
});

How do I use findOne() to get the object from players which is an array

Im currently trying this code but it returns null

const doc = await List.findOne({ players: { ID: 'id' } });

basically players is an array and I want to find ID from the players array of objects and get the document.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

db.collection.find({
  "players.ID": "1"
},
{
  "players.$": 1
})

mongoplayground


db.collection.find({
  "players.ID": "1"
})

mongoplayground


db.collection.aggregate([
  {
    $match: {
      "players.ID": "1"
    }
  },
  {
    $unwind: "$players"
  },
  {
    $match: {
      "players.ID": "1"
    }
  }
])

mongoplayground

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!