当我尝试查询时,我正在使用express和mongoose从mongodb检索数据:
Event.find({"StartTime":{$gte:start, $lte: end }}, function(err, today){
cb({"all":docs, "live_total": docs.length, "today":today, "today_total": today.length, "start":start, "end":end, "now":now});
})
它会返回结果,但是当我这样做时:
Event.find({"League":"2399913"}, function(err, ev){
console.log(ev)
})
即使我在mongodb中手动执行查询返回结果,它也不返回任何内容:
在mongo上查询
db.events.find({"League": "50012238"});
结果
{ "_id" : ObjectId("58c02fc1a683a6595444b5d0"), "EventID" : "62332340", "League" : "50012238", "LeagueName" : "King's Cup", "Sport" : "6046", "SportName" : "Football", "Location" : "80", "LocationName" : "Saudi Arabia", "StartTime" : ISODate("2017-01-21T07:15:00Z"), "homeTeam" : "50038781", "homeTeamName" : "Al Safa (KSA)", "awayTeam" : "250417", "awayTeamName" : "Al Qadisiya", "EventStatus" : "NSY", "Active" : "I", "BookmakerTotalCnt" : "3", "Outcomes" : { "Outcome" : { "OutcomeID" : "1", "OutcomeName" : "1X2", "Bookmaker" : { "BookmakerID" : "8", "BookmakerName" : "Bet365", "BookmakerTotalOddsCnt" : "18", "Odd" : [ { "EventID" : "62332340", "LineID" : "586978", "OutcomeID" : "1", "BookmakerID" : "8", "OutcomeName" : "1X2", "BookmakerName" : "Bet365", "bet" : "1", "BaseLine" : null, "line" : null, "CurrentPrice" : "6", "isResulting" : "-", "isWinner" : "0", "OutcomeType" : "1", "DrawName" : "X", "LineInfo" : null, "LineStatus" : "Open" }, { "EventID" : "62332340", "LineID" : "586979", "OutcomeID" : "1", "BookmakerID" : "8", "OutcomeName" : "1X2", "BookmakerName" : "Bet365", "bet" : "2", "BaseLine" : null, "line" : null, "CurrentPrice" : "1.4", "isResulting" : "-", "isWinner" : "0", "OutcomeType" : "1", "DrawName" : "X", "LineInfo" : null, "LineStatus" : "Open" }, { "EventID" : "62332340", "LineID" : "586980", "OutcomeID" : "1", "BookmakerID" : "8", "OutcomeName" : "1X2", "BookmakerName" : "Bet365", "bet" : "X", "BaseLine" : null, "line" : null, "CurrentPrice" : "4.333", "isResulting" : "-", "isWinner" : "0", "OutcomeType" : "1", "DrawName" : "X", "LineInfo" : null, "LineStatus" : "Open" } ] } } } }
这是我的模型定义:
var mongoose = require('mongoose');
var model = {};
//static method
exports.schema = function(req, res){
return mongoose.Schema({
Sport: Number,
League: Number,
LeagueName: String,
Location: String,
SportName: String,
StartTime: Date,
StartTimeJS: Date,
LocationName: String,
homeTeam: String,
homeTeamName: String,
awayTeam: String,
awayTeamName: String,
EventStatus: String,
Active: Number,
BookmakerTotalCnt: String,
Outcomes: Array,
EventID: Number
});
}
所以基本上它通过某些键找到但不是其他人,任何想法?
答案 0 :(得分:0)
手动执行了正确的查询:
db.events.find({"League": "50012238"});
但是在这个问题中" U"进入"Lueague"
是错误的!
Event.find({"Lueague":"2399913"}, function(err, ev){
console.log(ev)
})
试试这个:
Event.find({"League": 2399913}).exec(function(err, ev)
{ console.log(ev) });
答案 1 :(得分:0)
问题可能是你告诉Mongoose League
是一个数字,但它似乎以字符串的形式存储在数据库中。
因此,Mongoose运行相当于此查询(因为Mongoose会将查询中的字符串"2399913"
强制转换为数字,因此它与模式类型匹配):
db.events.find({ League : 2399913 })
注意数字周围没有引号。
因为字符串和数字是不同的,所以此查询将与您集合中的任何文档都不匹配。
修复方法是更新您的架构,以便League
成为字符串:
League: String,