1

I have documents that have the following structure:

{
    "somekey1": "somevalue1",
    "data" : [
        [
            {
                "value" : 0.807689530228178,
                "unit" : "mL"
            },
            {
                "value" : 0.7392892800962352,
                "unit" : "mL"
            },
        ],
        [
            {
                "value" : 0.8314139708574444,
                "unit" : "mL"
            },
            {
                "value" : 0.09766834482756892,
                "unit" : "mL"
            }
        ],
        [
            {
                "value" : 0.3821786847386669,
                "unit" : "mL"
            },
            {
                "value" : 0.18408410591658442,
                "unit" : "mL"
            }
        ]
   ]
}

What is the most efficient way to return all value/unit objects with value in a certain range? (ex. > 0.7?) Tried searching but didn't come up with anything helpful. Can it be done with a single find or aggregate operation?

2
  • What is your expected result? please give an example Commented Nov 24, 2016 at 2:35
  • { "value" : 0.807689530228178, "unit" : "mL" }, { "value" : 0.7392892800962352, "unit" : "mL" }, { "value" : 0.8314139708574444, "unit" : "mL" } } Commented Nov 24, 2016 at 14:38

1 Answer 1

1

You could use aggregation to do it:

db.collection.aggregate([{
  $unwind: '$data',
}, {
  $unwind: '$data',
}, {
  $match: {
    'data.value': {
      $gt: 0.7
    }
  }
}, {
  $project: {
    _id: 0,
    value: "$data.value",
    unit: "$data.unit",
  },
}])
Sign up to request clarification or add additional context in comments.

Comments

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.