mark
mark

Reputation: 737

$in for multiple values

var footballerIdToPassive = "qqqqq";
var footballerlevelToPassive = ["3052002","3052003"];

db.goals.find({ "footballer": footballerIdToPassive, "footballerlevel": { $in: [footballerlevelToPassive]}}))

for that it brings nothing but in goals db shell, when i run count of this, count is 1

{ "footballer": "qqqqq", "footballerlevel": "3052002" }

also for

var footballerlevelToPassive = "3052002";

this, it works. but i cant do for multiple. HOw can i do this work?

i need to use each , i need to update each one

Upvotes: 1

Views: 37

Answers (1)

chridam
chridam

Reputation: 103445

footballerlevelToPassive is already an array, there's no need to wrap it in another array again in your query, just reference it directly with $in as

var footballerIdToPassive = "qqqqq";
var footballerlevelToPassive = ["3052002","3052003"];

db.goals.find({ 
    "footballer": footballerIdToPassive, 
    "footballerlevel": { "$in": footballerlevelToPassive }
})

which is equivalent to the query

db.goals.find({ 
    "footballer": footballerIdToPassive, 
    "$or": [
        { "footballerlevel": "3052002" },
        { "footballerlevel": "3052003" }
    ]
})

Quick demo

enter image description here

Upvotes: 3

Related Questions