Error: $in needs an array

The structure is like this:

user = {
                    '_id': email,  
                    'username': username,
                    'password': hashed,
                    'images': {
                            'tags': []
                    }
            }

I added the images to the MongoDB database using GridFS and now I’m trying to recover them like this:

user = db.users.find_one({'username': username})
cursor = db.fs.files.find({'_id': {'$in': user['images']}})

But I get an error: $in needs an array. How can I fix this?

Hi @cosmina,
The. $in needs an array however user['images'] is an object. Should images contain an array of Ids?

I would expect then the structure to be:

user = {
                    '_id': email,  
                    'username': username,
                    'password': hashed,
                    'images': [ 'xxxxxxxxxxxx', ....]
            }

Best
Pavel

Yes, but I also need the ‘tags’ array within images. Can images still be an array in that case?

Hi @cosmina,

Not sure I understand the data design. Perhaps what you are looking for is an array of compound documents :

 'images': [{ id: 'xxxxxxxxxxxx', tags :[ ... ]}, ...}

You will need to map and push the ids into a new array of only ids to use the $in because it looks for field : { $in : [array of values to match]}

Best
Pavel