2021年1月18日星期一

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.

https://stackoverflow.com/questions/65783545/wildcard-for-key-in-mongodb-query January 19, 2021 at 07:35AM

没有评论:

发表评论