Search code examples
mongodbbsonmongodb-ruby

$all not working with array


I am trying to get all submissions documents that contain all of the tags listed in an array.

My current code looks like this:

submissions.find({ tags: { $all => tags } })
print tags.count

However, upon running, I receive the following error on the second line:

BSON::InvalidKey at /
NilClass instances are not allowed as keys in a BSON document.

It should also be noted that the following does work as it should (though it does not achieve my desired result):

submissions.find({ tags: tags })
print tags.count

How can I fix my original code to find the wanted documents properly?


Solution

  • The $all expression in MongoDB is:

    submissions.find({ tags: { $all: tags }});
    

    In this case, "tags" must be an array (tags = ['tag1','tag2','tag3'])

    Why do you used "$all => tags" instead "$all: tags" ?