我试图使用bluebird + mongoose分别查询每个俱乐部的DevHiin实例,如下面的代码:
DevHiin.clubs的数据结构是:
, clubs : [{ type: Schema.Types.ObjectId, ref: 'Club' }]
和club_M是俱乐部模式的一个实例:
User.findOne({"_id": req.user._id})
.populate('clubs', '-textIndex')
.exec(function (err, user) {
if (err) res.status(500).json(err);
if (user.clubs) {
var clubs_M = user.clubs;
for (c = 0; c < clubs_M.length; c++) {
var club_M = clubs_M[c];
if (club_M.lst > lstDate) {
serverData.clubs.push(club_M);
var ciinPromise = DevHiin.find({"clubs": {$elemMatch: club_M}, "ts": {"$gt": lastSyncTime}})
.populate('uId', '_id username', null, null) // get necessary user info for a hiin
.lean()
.sort("ts")
.limit(10)
.execAsync();
promises.push(ciinPromise);
}
}
Promise.all(promises).then(function (ciinArrays) {
serverData.ciinArrays = ciinArrays;
callback(ciinArrays);
}).catch(function (err) {
callback(err);
});
不幸的是,我收到了以下错误信息:
"Error: Can't use $__ with Array.
at SchemaArray.castForQuery (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/lib/schema/array.js:188:13)
at SchemaArray.cast$elemMatch (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/lib/schema/array.js:336:23)
at SchemaArray.castForQuery (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/lib/schema/array.js:191:19)
at module.exports (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/lib/cast.js:196:39)
at Query.cast (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/lib/query.js:2350:10)
at Query.find (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/lib/query.js:999:10)
at Query.exec (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/lib/query.js:1984:17)
at Query.tryCatcher (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/bluebird/js/main/util.js:24:31)
at Query.ret [as execAsync] (eval at <anonymous> (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/bluebird/js/main/promisify.js:1:0), <anonymous>:12:23)
at EventEmitter.<anonymous> (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/controllers/sync.js:189:18)
at EventEmitter.<anonymous> (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/node_modules/mpromise/lib/promise.js:175:45)
at emitOne (events.js:77:13)
at EventEmitter.emit (events.js:169:7)
at Promise.safeEmit (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/node_modules/mpromise/lib/promise.js:81:21)
at Promise.fulfill (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/node_modules/mpromise/lib/promise.js:94:24)
at Promise.resolve (/Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/lib/promise.js:113:23)
at /Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/lib/query.js:1174:16
at /Users/vince-fan/WorkSpaces/MEAN/V_Starter/node_modules/mongoose/node_modules/kareem/index.js:109:16
at doNTCallback0 (node.js:408:9)
at process._tickCallback (node.js:337:13)"
这是蓝鸟承诺错误还是猫鼬错误?我该如何解决?
答案 0 :(得分:1)
从代码中看起来,您尝试使用 $elemMatch
对指定为查询条件表达式的数组值进行查询。我相信你需要使用 $in
,这会选择club_M
字段的值等于指定数组中任何值的文档,该数据由变量存储在你的情况下var ciinPromise = DevHiin.find({"clubs": {"$in": [club_M._id]}, "ts": {"$gt": lastSyncTime}})
。因此,您的查询应如下所示:
oBook.Worksheets(1).Range("$D$1:$D$4000").AutoFilter(Field:=4, Criteria:="#N/A")
答案 1 :(得分:0)
for querying field包含一个数组,简单地将其视为普通字段查询。
我的固定代码:
FileInputStream file = new FileInputStream(new File("C:\\testdata.xls"));
HSSFWorkbook workbook = new HSSFWorkbook(file);
HSSFSheet sheet = workbook.getSheetAt(0);
for (int i=1; i <= sheet.getLastRowNum(); i++){
String keyword = sheet.getRow(i).getCell(0).getStringCellValue();
searchbox.sendKeys(keyword);
searchbox.submit();
driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
}
workbook.close();
file.close();