0

I have document structure like this in my schools collection MongoDB document

I am searching school by its code from array of codes School.findOne({ 'codes.code': '12345678' }) and I am getting my school but I also want to get code.type property from code object where my code is 12345678. Should i use javascript Array.find?

1 Answer 1

1

What you can do is selecting the matching array element by

School.find({ 'codes.code': '12345678' }, {"codes.$":1})

Otherwise, yes, you need to find the element in the array:

var myschool = School.find({ 'codes.code': '12345678' })[0]
myschool.codes.find(c => c.code == '12345678')
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.