ojon
ojon

Reputation: 353

Wildcard for key in mongodb query

I have a collection equivalent to:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "sides": {
      "0": {
        "dist": 100
      },
      "1": {
        "dist": 10
      }
    }
  },
  {
    "_id": ObjectId("5a934e000102030405000001"),
    "sides": {
      "0": {
        "dist": 100
      }
    }
  }
]

I would like to perform a query that return any documents that has for any key nested in sides has the key dist with a specific value. Something like:

db.collection.find({"sides.*.dist": 10})

Here * acts as a wildcard, any key would be valid in its place.

That would retrieve:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "sides": {
      "0": {
        "dist": 100
      },
      "1": {
        "dist": 10
      }
    }
  }
]

On the other hand

db.collection.find({"sides.*.dist": 100})

Would retrive both documents.

Upvotes: 2

Views: 1147

Answers (2)

Gibbs
Gibbs

Reputation: 22964

You could get the matching elements using this

db.collection.aggregate([
  {
    "$project": {
      "sides_array": {//Reshape the sides
        "$objectToArray": "$sides"
      }
    }
  },
  {//Denormalize to get more than one matches
    "$unwind": "$sides_array"
  },
  {
    "$match": {//Condition goes here
      "sides_array.v.dist": 10
    }
  },
  {
    "$group": {//Group the data back, after unwinding
      "_id": "$_id",
      "sides": {
        "$push": "$sides_array"
      }
    }
  },
  {
    "$project": {//Reshape the data
      "_id": 1,
      "sides": {
        "$arrayToObject": "$sides"
      }
    }
  }
])

Upvotes: 2

Dĵ ΝιΓΞΗΛψΚ
Dĵ ΝιΓΞΗΛψΚ

Reputation: 5669

the following song and dance won't be neccessary if sides field was an array...

db.collection.find(
{
    $expr: {
        $gt: [{
            $size: {
                $filter: {
                    input: { $objectToArray: "$sides" },
                    as: "x",
                    cond: { $eq: ["$$x.v.dist", 10] }
                }
            }
        }, 0]
    }
})

Upvotes: 2

Related Questions