Sunday, 12 March 2017

MongoDB: Checking if nested array contains sub-array

I have a use case where the database is modelled like this:

name: XYZ
gradeCards: [{
  id: 1234, // id of the report card
  comments: ['GOOD','NICE','WOW']
}, {
  id: 2345,
  comments: ['GOOD','NICE TRY']
}]

Now, I have a query that I would like to query the schema as follows:

I would be given a list of ids and values. For example: the given list is as follows:

[{
  id: 1234,
  comments: ['GOOD','NICE']
},{
  id: 2345,
  comments: ['GOOD']
}]

In short the ID should be matching and the comments should be a sub-array of the comments array for that id and also, all the conditions specified in the array should be matched, so it should be an AND condition on all the conditions provided.



via Maulik Soneji

No comments:

Post a Comment