2

Say I have things in database with this schema:

{
    tags: [String]
}

I have tags array like this: ["tag1", "tag2"]
now I need to search in things and get only those which contains my tags.

I've searched and found the $where operator here but it's not what I need. I was wondering there should be something like
db.things.find( { $where: "this.tags.contains(<my tags somehow>)" } ); in mongo or something like this in mongoose. Any ideas?

shift66
  • 11,760
  • 13
  • 50
  • 83
  • possible duplicate of [Find document with array that contains a specific value](http://stackoverflow.com/questions/18148166/find-document-with-array-that-contains-a-specific-value) – Yves M. Jan 27 '14 at 17:11

2 Answers2

3

You want $all.

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24all

Here are a couple of examples from mongoose: https://github.com/LearnBoost/mongoose/blob/master/test/query.test.js#L396

On a side note, $where is typically not scalable.

Eve Freeman
  • 32,467
  • 4
  • 86
  • 101
2

MongoDB query syntax allows you to match an element array like that:

.find( { tags: 'tag1' } )

And, of course, it's working with Mongoose :)

Source: http://docs.mongodb.org/manual/tutorial/query-documents/#match-an-array-element

Yves M.
  • 29,855
  • 23
  • 108
  • 144