0

I Have been trying several methods in the Mongo documentation manually and it does not seem to solve my issue. Here is a structure of my data for reference

"loopback": [
        {
            "_date": "some date",
            "dutTestParams": [],
            "_id": "5f680665dfbfb74e78cae175",
            "paramId": "dummyid",
            "jtagApbStatus": "true",
            "dutYaml": "dummy_yaml",
            "dutSequenceId": "dummy_sequence",
            "dutGitVersion": "1.0.1",
            "guiVersion": "1.0",
            "projectId": "alphacore",
            "jobId": "id",
            "pvt": "dummypvt",
            "rate": 1,
            "refclk": 1,
            "channelLoss": 1,
            "voltage1": 1,
            "voltage2": 0.5,
            "voltage3": 0.5,
            "temp": 10,
            "txwidth": 8,
            "rxwidth": 8,
            "name": "loopback",
            "data": [
                {
                    "onLane": [
                        1
                    ],
                    "_id": "5f680675dfbfb74e78cae17c",
                    "chpid": "chip1",
                    "loopbackname": "BER",
                    "loopbackvalue": 0,
                    "laneid": 1,
                    "iteration": 1
                },
                {
                    "onLane": [
                        1
                    ],
                    "_id": "5f680675dfbfb74e78cae17d",
                    "chipid": "chip2",
                    "loopbackname": "BER",
                    "loopbackvalue": 0,
                    "laneid": 1,
                    "iteration": 1
                },{....}, ....., {....}]  

Basically i have a parent document. There is test data stored in the parent header document. This data is related to the test, so it does not necessarily have to be BER. It can be other test points. Further, it contains all the chips tested in the test.

I can filter the data once queried, this is trivial. However, is there any way I can make a specific query? What I ideally want is to be able to do something along these lines:

db.testdata.find({jobid:"some id", data.0.chipid:"chip1"})

so the query returns the header parent document and only sub documents that have chipid == chip1. When I attempt something along these lines, I get all sub documents, which is not what I want.

Any experienced help will be very much appreciated!

1 Answer 1

1

Use this query -

db.collection.aggregate([
  {
    "$match": {
      jobId: "id"
    }
  },
  {
    "$unwind": "$data"
  },
  {
    "$match": {
      "data.chipid": "chip1"
    }
  }
])

Try it on Mongo Playground

Sign up to request clarification or add additional context in comments.

3 Comments

thanks man will upvote. I am assuming this general flow works for anything in the sub documents?
Yes it does work. Try it in playground. Accept the answer if it works.
Consider accepting the answer if it worked for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.