pymongo.errors.OperationFailure: $in 需要一个数组
pymongo.errors.OperationFailure: $in needs an array
我收到错误 pymongo.errors.OperationFailure: $in 需要一个数组
而 运行 以下代码:-
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)
任何人都可以帮助我为什么会收到此错误。
我试过写:-
output = list(db.networkSetting.find({'networkId':{'$in':list(id['_id'])},'locationRegion.wirelessCountryRegion':'JP'},{'networkId':1}))
但后来又说 ObjectId 不可迭代。
谁能解决问题。
$in
需要一个数组,但是你的过滤器只需要一个简单的字符串搜索,所以你的查询应该是:
output = list(db.networkSetting.find({'networkId': id['_id'], 'locationRegion.wirelessCountryRegion': 'JP'}, {'networkId': 1}))
我收到错误 pymongo.errors.OperationFailure: $in 需要一个数组 而 运行 以下代码:-
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)
任何人都可以帮助我为什么会收到此错误。 我试过写:-
output = list(db.networkSetting.find({'networkId':{'$in':list(id['_id'])},'locationRegion.wirelessCountryRegion':'JP'},{'networkId':1}))
但后来又说 ObjectId 不可迭代。 谁能解决问题。
$in
需要一个数组,但是你的过滤器只需要一个简单的字符串搜索,所以你的查询应该是:
output = list(db.networkSetting.find({'networkId': id['_id'], 'locationRegion.wirelessCountryRegion': 'JP'}, {'networkId': 1}))