'MongoDB How to define a query for find an object in array, but the array exist of Object references

Basically what I want to do is that I have an ObjectId of a qr. I want to use that ObjectId to find out which qrBlock does it belong. Im leaving my dbmodel here so you can track easily.

qrRoute.post("/scanQr", (req, res) => {
  let { data } = req.body;
  var id = mongoose.Types.ObjectId(data);
  Qr.findById(id)
    .exec()
    .then((qrr) => {
      QrBlock.find({ qr: { "qr.$": id } }, (qrblck) => {
        console.log(qrblck);
      });

      
    });
});

I tried this code above it didn't work.



Solution 1:[1]

QrBlock.findOne({ qr: { $all: [qrr._id] } })

this worked for me

Solution 2:[2]

did you try this way

qrRoute.post("/scanQr", (req, res) => {
  let { data } = req.body;
  var id = mongoose.Types.ObjectId(data);
  Qr.findById(id)
    .exec()
    .then((qrr) => {
      QrBlock.find({ qr: id }, (qrblck) => {
        console.log(qrblck);
      });

      
    });
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ziya Karagöz
Solution 2 Smriti Shikha