Petr Vasilev
Petr Vasilev

Reputation: 150

Mongoose query through the element inside the array

I have a mongoose model:

let schema = new Schema({

    email: {
        type: String,
        required: true,
        unique: true
    },
    password: {
        type: String
    },
    username: {
        type: String,
        unique: true
    },
    confirmed: {
        type: Boolean
    },
    payload: [{
        type: {
           token: blablabla,
           type: blablabla
        }
    }]

});

And i want find user by payload.token. How can I do that? I tried $elemMatch, but it does not work.

Upvotes: 0

Views: 523

Answers (2)

Supradeep
Supradeep

Reputation: 3266

If payload is an array of objects and you want to find users by token value , below query should work :

db.users.find({payload: {$elemMatch: {'type.token':'blablabla'}}});

Upvotes: 0

Rudi
Rudi

Reputation: 2995

You can do

.find({'payload.type.token': token})

Upvotes: 1

Related Questions