mardi 31 octobre 2017

Mongodb query on nested members with unknown keys

So I have objects which have thumbnail images that are stored like this under hashed keys:

{
    "_id" : xxxxxxxxx,
    "thumbnails" : {
        "dac898610078d3c8c277562902be79a7" : {
            "1500" : "http://ift.tt/2zm1r5Y",
            "1260" : "http://ift.tt/2iPp7ZC",
            "322x181" : "http://ift.tt/2zm1rD0"
    },
        "619b3999ed549a2fbc569d774ca921c4" : {
            "595x595" : "http://ift.tt/2iT4Cvb"
    }
}

(The different hashes are for desktop version/mobile version.) I introduced a bug into the image processing code for about a week that saved cropped and resized images as (and wrote to the db as)

{
    "thumbnails": {
        "dac898610078d3c8c277562902be79a7" : {
            "50x50" : "http://ift.tt/2zl3Ldn",
    }
}

(note the erroneously repeated TLD/bucketname).

Question is this: I'm trying to find a regex-based query to find all the places where these wrong URLs got saved to the database. My list of attempts is kind of embarrassing, so I'm not going to list them all here unless it's really wanted, but I think this is getting close (wide open regex because testing):

db.contentobjects.find({"thumbnails": { $elemMatch : { $regex : /.*/} } } )

This returns naught. Problem is I need to regex the key, not the val. Figuring out the search for the repeated TLD is a separate problem I'll deal with anon; right now I just want to find all the hash-based keys under

{
    "thumbnails" : {
        "*" : {}
    }
}

in a large collection of objects.




Aucun commentaire:

Enregistrer un commentaire