Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to conditionally find records from mongodb using node

[ 
  {
   name: 'Some one'
   rep_id: '101',
   user_ids: ['102','103','104']
  },
  {
   name: 'Some one else'
   rep_id: '106',
   user_ids: ['102','103','104','105']
  },
]

I want to search records with rep_id from mongodb. If rep_id is not matched it should search the same id in user_ids array. If it’s not present in user_ids array as well, it should skip that record. I’m able to find with rep_id being matched but how can I do the same if it’s not matched and it’s present in user_ids array.
From the above records, if rep_id is 101 it should give me the first record and if rep_id is 102 it should give me the both the records since 102 is present in both user_ids array.

Even a hint of the solution will be enough

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

use $or

db.collection.aggregate([
  {
    "$match": {
      "$or": [
        {
          rep_id: "102"
        },
        {
          user_ids: "102"
        }
      ]
    }
  }
])

mongoplayground

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading