Reputation: 165
I am getting error as pymongo.errors.OperationFailure: $in needs an array while running the following code:-
rom pymongo import MongoClient
from bson.dbref import DBRef
from bson.objectid import ObjectId
import sys
sys.path.append('/etc/insight')
import constants
constant = constants.Constants()
# Create MongoDB Connection
mongoconn = MongoClient(constant.MONGO_HOST, readPreference=constant.READ_PREFERENCE)
db = mongoconn[constant.MONGO_USERNAME]
# Mongo authentication to connect to database
auth = db.authenticate(constant.MONGO_USERNAME, constant.MONGO_PASSWORD)
# Query in network
nw = list(db.network.find({'orgId':{'$exists':True}},{'_id':1}))
print(nw)
# Query in networkSetting
networkIds = []
for id in nw:
output = list(db.networkSetting.find({'networkId':{'$in':id['_id']},'locationRegion.wirelessCountryRegion':'JP'},{'networkId':1}))
networkIds.append(output)
print(networkIds)
Can anyone please help me why I am getting this error. I have tried writing:-
output = list(db.networkSetting.find({'networkId':{'$in':list(id['_id'])},'locationRegion.wirelessCountryRegion':'JP'},{'networkId':1}))
But then it is saying that ObjectId is not iterable. Can anyone solve the problem.
Upvotes: 0
Views: 1254
Reputation: 8834
$in
requires an array, but your filter only needs a simple string search, so your query should be:
output = list(db.networkSetting.find({'networkId': id['_id'], 'locationRegion.wirelessCountryRegion': 'JP'}, {'networkId': 1}))
Upvotes: 1