1
var campgroundSchema = new mongoose.Schema({
    likey:{
        numOfLikeys:{
            type:Number,
            default:0
        },
        whoLiked:[{
            id:{
                type:mongoose.Schema.Types.ObjectId,
                ref:"User"
            },
            username:String
        }]
    }
});

I want to search for "username" inside of the whoLiked array. I tried this: Campground.find({ likey.whoLiked.username: req.user.username }, ...);

Jinwook Kim
  • 523
  • 4
  • 11
  • put double quotes around `"likey.whoLiked.username"` – Ashh Aug 25 '18 at 04:42
  • 1
    Possible duplicate of [Find in Double Nested Array MongoDB](https://stackoverflow.com/questions/29071748/find-in-double-nested-array-mongodb) – Ashh Aug 25 '18 at 04:44

1 Answers1

0

It should be:

Campground.find({ "likey.whoLiked.username": req.user.username })

Where "likey.whoLiked.username" is wrapped as string to indicate the entire search path.

Make sure req.user.username is an actual valid value as well.

Akrion
  • 18,117
  • 1
  • 34
  • 54