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 find a record by unique value inside an array in MongoDB?

I want to locate a record which has the "String to match" in his "affiliates.wallets" array. But for some reason, I’m unable to do it.

I was trying something like:

db.collection.findOne({ "affiliates.wallets": {$in: "[String to match]"}, (err, data) => console.log(err, data)})

But it returns null. So, I’m asking for help.

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

Simplified Wallet Schema:

Wallet {
  affiliates: {
    wallets: [String]
  }
}

P.S: I’m using findOne method because this "String to match" which I’m using to locate the record is unique and it can be located only in one record.

>Solution :

You don’t need $in operator. You can do it like this:

db.collection.findOne({
  "affiliates.wallets": "String to match", 
}, (err, data) => {
  console.log(err, data)
})
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