PlankTon
PlankTon

Reputation: 12605

Mongo: Array field has no values in given array?

I'm trying to write up a mongo query that finds all entries where the "steps" field has no values within an array argument.

So for example, given two entries with values:

Entry1:
  steps: [3, 4]

Entry2:
  steps: [3, 5]

The query should return entry1, but not entry 2, for input array [4, 8, 10]. I'm quite new to mongo - any ideas appreciated.

Upvotes: 0

Views: 90

Answers (1)

Nuk Nuk San
Nuk Nuk San

Reputation: 560

You mean you have some records:

db.foo.find() { "_id" : 1, "steps" : [ 3, 4 ] } { "_id" : 2, "steps" : [ 3, 5 ] }

Then you would query:

> db.foo.find({steps:{$in:[4,8,10]}})
{ "_id" : 1, "steps" : [ 3, 4 ] }

the $in clause will pick records in which any stored element matches any of terms in the array supplied in the query

Upvotes: 1

Related Questions