Home > Net >  How to find object in array of objects mongoose
How to find object in array of objects mongoose

Time:07-25

I have this schema (groupschema)

const mongoose = require('mongoose')

const GroupSchema = new mongoose.Schema({
CreatedBy:String,
Name:String,
Link:String,
Password:String,
Periods:Number,
Members:Array,

})

module.exports = mongoose.model('groups', GroupSchema, 'groups')

And Members is an array of objects. This is how members is stored.

   Members[ {
    Email:req.user.email,
    DisplayName:displayname,
    GoogleId:req.user.googleId,
    Image:req.user.image,
    }]

I want to find the each user's object with their googleId. How do I find their objects with just their googled looking through every single group document?

I've tried this, but it keeps returning an empty array.

 const datafound = await groupschema.find({ "Members": { googleId: req.user.googleId } });

Thank you

CodePudding user response:

I think you're typo, it should be GoogleId

const datafound = await groupschema.find({ "Members": { GoogleId: req.user.googleId } });
  • Related