MongoDB.live, free & fully virtual. Register Now MongoDB.live, free & fully virtual. Register Now

Find and load from array only specific data

Hello, i have 2 arrays - example:

array1 = [ 'one', 'two', 'three', 'four', 'five', 'six', 'seven' ]
array2 = [ '1', '2', '3', '4', '5', '6', '7' ]

i need load not all array, only that i need - Example only - two or 4

Try

    $match : { 
         $or: [ { array1 : { $elemMatch: { $eq: 'two' } } }, { array2 : { $elemMatch: { $eq: '4' } } } ]
     }

How i can do it?

you can try $filter i think it would solve your problem.

if the array has a million records, I want to reduce the load

Having

is a bad design decision. Please see https://developer.mongodb.com/article/schema-design-anti-pattern-massive-arrays.

But, indeed as mentioned by @Abhishek_Dhadwal, you should also look at https://docs.mongodb.com/manual/reference/operator/aggregation/filter/ if reworking your schema is out of the question.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.