Skip to content
Advertisement

MongoDB Extract document

I have this Json:

JavaScript

I would like to filter exclusive for paymentType.type equals “PILOT” and limits equals type equals “COMBAT_HOURS”.

This is the result I would like to receive.

JavaScript

How to extract the document to reach this result?

Advertisement

Answer

Query

  • map paymentTypes
  • if paymentType= PILOT, filter the limits to keep only the combat hours
  • else null (we don’t want that paymentType)
  • remove the null values (those null were paymentTypes that weren’t of type PILOT)

*this can cause empty arrays, either on paymentTypes or on limits,if you dont accept empty arrays, you can match and remove the documents with those empty arrays

PlayMongo

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement