0

There are 79 parameters in each experiment/document . There are 27 experiment/documents.

{
  _id: 0,
  experiment: 1,
  parameters: [
    {
      name: "clock",
      value: 8,
      type: "system"
    },
    {
      name: "B",
      value: 100000,
      type: "puls"
    },
    {
      name: "campaign",
      value: "October2019",
      type: "navi"
    }
  ]
}

I would like to have all the experiment/ documents where- name : "B" > value : 1000 and name : "campaign" = value : "October2019"

The output should look like this: https://mongoplayground.net/p/UP39-Yxk61U

Anat
  • 1
  • 1
  • Does this answer your question? [Retrieve only the queried element in an object array in MongoDB collection](https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection) – AlexisG Feb 21 '22 at 12:43
  • The solution attached refers to one condition. However I'm trying to retrieve an answer that includes an intersection of several condition. eg. : name : "B" > value : 1000 and name : "campaign" = value : "October2019 – Anat Feb 21 '22 at 15:15
  • You can use a `$and` in the cond parameter of the `$filter` – AlexisG Feb 21 '22 at 15:17
  • In which way? how should I combine the several conditions? Could you please provide an example? – Anat Feb 21 '22 at 15:37
  • What is your desired output? ... the entire document where there is a match? ... just the parts of each document where there is a match? ... something else? – rickhg12hs Feb 21 '22 at 20:20
  • The output should be several parameters including their name, value and type. eg. "B", "campaign" , and including the experiment id. – Anat Feb 21 '22 at 23:57
  • Perhaps it would be best if you create a [mongoplayground.net](https://mongoplayground.net/) configuration and edit your question to show exactly the output you desire. – rickhg12hs Feb 23 '22 at 03:07
  • The output should look like this: https://mongoplayground.net/p/UP39-Yxk61U – Anat Feb 23 '22 at 23:24

1 Answers1

0

Here is a solution where I use $filter to filter the content of the parameters field

And I use a combination of $and and $or, to recreate your conditions

((name = "B") AND (value > 1000)) OR ((name = "campaign") AND (value = "October2019"))

try it here

db.collection.aggregate([
  {
    $project: {
      _id: 1,
      experiment: 1,
      parameters: {
        $filter: {
          input: "$parameters",
          as: "parameter",
          cond: {
            $or: [
              {
                $and: [
                  {
                    $eq: [
                      "$$parameter.name",
                      "B"
                    ]
                  },
                  {
                    $gt: [
                      "$$parameter.value",
                      1000
                    ]
                  }
                ]
              },
              {
                $and: [
                  {
                    $eq: [
                      "$$parameter.name",
                      "campaign"
                    ]
                  },
                  {
                    $eq: [
                      "$$parameter.value",
                      "October2019"
                    ]
                  }
                ]
              },
              
            ]
          }
        }
      },
      
    }
  }
])
AlexisG
  • 2,476
  • 3
  • 11
  • 25
  • for some reason the output is an empty list/array of parameters. Also how can I switch this condition $eq: [ "$$parameter.value", "October2019" to: contains "Oct"? – Anat Feb 22 '22 at 00:23
  • I also changed this condition to be "AND" instead of "OR" in the middle: ((name = "B") AND (value > 1000)) AND ((name = "campaign") AND (value = "October2019")) – Anat Feb 22 '22 at 00:30
  • name can't be equal to "B" and equals to "campaign" at the same time, and the value can't be equal to "October2019 " while being greater than 1000. There is something wrong with the condition – AlexisG Feb 22 '22 at 04:41
  • Ho can I implement an intersection of the conditions? I would like to retrieve the documents where "B>"1000 AND "campaign" contains "Oct" . Is there another way? – Anat Feb 22 '22 at 09:21