Ethan McTague
Ethan McTague

Reputation: 2358

$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?

Upvotes: 0

Views: 267

Answers (1)

Rafael Schettino
Rafael Schettino

Reputation: 109

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" ?

Upvotes: 1

Related Questions