我有一个名为'indexes'的集合,其中包含'symbol','price'和'timestamp'字段。MongoDB查询日期条件没有结果,MongoDB Shell工程
我想查询这个集合的项目与特定'符号',并且有一个时间戳大于某些minDate值。
当通过Mongoose查询数据时,我没有得到任何结果,当我有'时间戳'的条件。然而,查询在MongoDB shell中运行。
我创造了我的收藏与下面的模式:
var IndexSchema = new Schema({
symbol: {
type: String
},
price: {
type: Number
},
timestamp: {
type: Date,
default: Date.now
}
});
在我的NodeJS应用我查询数据是这样的:
var Indexes = mongoose.model('Index');
var numDays = 7;
var minDate = new Date();
minDate.setDate(minDate.getDate() - (numDays));
Indexes
.find({'symbol':indexSymbol, 'timestamp': {$gte: minDate} })
.sort({'timestamp': 1});
.exec(function (err, items) {
console.log("There were %s items returned", items.length); // items.length == 0.
});
只要运行该查询,不论日期的使用,我回来0结果。
即使我尝试运行它的所有过去的条目(时间戳小于或等于当前时间),我仍然得到0结果。
var now = new Date();
Indexes
.find({'symbol':indexSymbol, 'timestamp': {$lte: now} })
.sort({'timestamp': 1});
.exec(function (err, items) {
console.log("There were %s items returned", items.length); // items.length == 0.
});
我知道我的收藏中存在的文件,如果我删除了“时间戳”的条件,并且查询只由“符号”,它返回(全部)的数据,符合市场预期。
Indexes
.find({'symbol':indexSymbol})
.sort({'timestamp': 1});
.exec(function (err, items) {
console.log("There were %s items returned", items.length); // items.length == ~40000.
});
在我的应用程序的NodeJS我试图格式化日期值作为ISO字符串,UNIX时间戳,并在引号包裹“$ GTE”。
// ISODate Format.
.find({'symbol':indexSymbol, 'timestamp': {$lte: minDate.toISOString()} })
// Unix Timestamp
.find({'symbol':indexSymbol, 'timestamp': {$gte: minDate.getTime() } })
// Wrapping '$gte' in quotes.
.find({'symbol':indexSymbol, 'timestamp': {'$gte': minDate } })
在我的NodeJS应用程序中使用所有这些,我仍然收回0个文档。
但是,我的查询按照我对MongoDB shell以及'Mongo Management Studio'的预期执行。
// MongoDB Shell Query using minDate.toISOString() value.
> db.indexes.find({'symbol':'.XBT', 'timestamp': { $gte: '2015-12-09T14:57:58.588Z' } });
{ "_id" : ObjectId("566841cff485eb63b3e10375"), "symbol" : ".XBT", "price" : 421.41, "timestamp" : "2015-12-09T14:58:00.000Z" }
{ "_id" : ObjectId("566841cff485eb63b3e10376"), "symbol" : ".XBT", "price" : 421.45, "timestamp" : "2015-12-09T14:59:00.000Z" }
{ "_id" : ObjectId("56684237f485eb63b3e10378"), "symbol" : ".XBT", "price" : 421.4, "timestamp" : "2015-12-09T15:00:00.000Z" }
{ "_id" : ObjectId("56684237f485eb63b3e10379"), "symbol" : ".XBT", "price" : 421.48, "timestamp" : "2015-12-09T15:01:00.000Z" }
{ "_id" : ObjectId("566842c0f485eb63b3e1037d"), "symbol" : ".XBT", "price" : 421.18, "timestamp" : "2015-12-09T15:02:00.000Z" }
{ "_id" : ObjectId("566842c0f485eb63b3e1037e"), "symbol" : ".XBT", "price" : 421.2, "timestamp" : "2015-12-09T15:03:00.000Z" }
{ "_id" : ObjectId("56684328f485eb63b3e10380"), "symbol" : ".XBT", "price" : 421.26, "timestamp" : "2015-12-09T15:04:00.000Z" }
{ "_id" : ObjectId("56684328f485eb63b3e10381"), "symbol" : ".XBT", "price" : 420.76, "timestamp" : "2015-12-09T15:05:00.000Z" }
{ "_id" : ObjectId("566843b0f485eb63b3e10384"), "symbol" : ".XBT", "price" : 420.47, "timestamp" : "2015-12-09T15:06:00.000Z" }
{ "_id" : ObjectId("566843b0f485eb63b3e10385"), "symbol" : ".XBT", "price" : 420.35, "timestamp" : "2015-12-09T15:07:00.000Z" }
{ "_id" : ObjectId("5668441af485eb63b3e10389"), "symbol" : ".XBT", "price" : 420.19, "timestamp" : "2015-12-09T15:08:00.000Z" }
{ "_id" : ObjectId("5668441af485eb63b3e1038a"), "symbol" : ".XBT", "price" : 420.09, "timestamp" : "2015-12-09T15:09:00.000Z" }
{ "_id" : ObjectId("566844a0f485eb63b3e1038d"), "symbol" : ".XBT", "price" : 420.22, "timestamp" : "2015-12-09T15:10:00.000Z" }
{ "_id" : ObjectId("566844a0f485eb63b3e1038e"), "symbol" : ".XBT", "price" : 420.25, "timestamp" : "2015-12-09T15:11:00.000Z" }
{ "_id" : ObjectId("56684507f485eb63b3e10390"), "symbol" : ".XBT", "price" : 420.27, "timestamp" : "2015-12-09T15:12:00.000Z" }
{ "_id" : ObjectId("56684507f485eb63b3e10391"), "symbol" : ".XBT", "price" : 420.21, "timestamp" : "2015-12-09T15:13:00.000Z" }
{ "_id" : ObjectId("5668458ef485eb63b3e10395"), "symbol" : ".XBT", "price" : 420.25, "timestamp" : "2015-12-09T15:14:00.000Z" }
{ "_id" : ObjectId("5668458ef485eb63b3e10396"), "symbol" : ".XBT", "price" : 420.17, "timestamp" : "2015-12-09T15:15:00.000Z" }
{ "_id" : ObjectId("566845f6f485eb63b3e10398"), "symbol" : ".XBT", "price" : 420.11, "timestamp" : "2015-12-09T15:16:00.000Z" }
{ "_id" : ObjectId("566845f6f485eb63b3e10399"), "symbol" : ".XBT", "price" : 420.16, "timestamp" : "2015-12-09T15:17:00.000Z" }
// MongoDB Shell Query using minDate.getTime() value.
> db.indexes.find({'symbol':'.XBT', 'timestamp': { $gte: '1449673802572' } });
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bb1"), "symbol" : ".XBT", "price" : 385.56, "timestamp" : "2015-11-07T19:21:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bb2"), "symbol" : ".XBT", "price" : 385.86, "timestamp" : "2015-11-07T19:22:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bb3"), "symbol" : ".XBT", "price" : 386.17, "timestamp" : "2015-11-07T19:23:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bb4"), "symbol" : ".XBT", "price" : 386.43, "timestamp" : "2015-11-07T19:24:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bb5"), "symbol" : ".XBT", "price" : 386.51, "timestamp" : "2015-11-07T19:25:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bb6"), "symbol" : ".XBT", "price" : 386.39, "timestamp" : "2015-11-07T19:26:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bb7"), "symbol" : ".XBT", "price" : 386.45, "timestamp" : "2015-11-07T19:27:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bb8"), "symbol" : ".XBT", "price" : 386.45, "timestamp" : "2015-11-07T19:28:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bb9"), "symbol" : ".XBT", "price" : 386.85, "timestamp" : "2015-11-07T19:29:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bba"), "symbol" : ".XBT", "price" : 386.91, "timestamp" : "2015-11-07T19:30:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bbb"), "symbol" : ".XBT", "price" : 387.08, "timestamp" : "2015-11-07T19:31:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bbc"), "symbol" : ".XBT", "price" : 387.29, "timestamp" : "2015-11-07T19:32:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bbd"), "symbol" : ".XBT", "price" : 387.29, "timestamp" : "2015-11-07T19:33:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bbe"), "symbol" : ".XBT", "price" : 387.47, "timestamp" : "2015-11-07T19:34:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bbf"), "symbol" : ".XBT", "price" : 387.42, "timestamp" : "2015-11-07T19:35:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bc0"), "symbol" : ".XBT", "price" : 387.56, "timestamp" : "2015-11-07T19:36:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bc1"), "symbol" : ".XBT", "price" : 387.77, "timestamp" : "2015-11-07T19:37:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bc2"), "symbol" : ".XBT", "price" : 387.86, "timestamp" : "2015-11-07T19:38:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bc3"), "symbol" : ".XBT", "price" : 387.91, "timestamp" : "2015-11-07T19:39:00.000Z" }
{ "_id" : ObjectId("5665dc4ff485eb63b3e04bc4"), "symbol" : ".XBT", "price" : 387.89, "timestamp" : "2015-11-07T19:40:00.000Z" }
我试着使用类似的查询生成器的语法来修改我的查询here
// Using the timestamp condition
Indexes
.find({})
.where('symbol').equals(indexSymbol)
.where('timestamp').gte(minDate)
.sort('timestamp')
.select('symbol price timestamp')
.exec(function (err, items) {
if(err) {
console.log("Error: %s", err);
} else {
console.log("There are %s items", items.length);
// There are 0 items.
}
});
// Without the timestamp condition
Indexes
.find({})
.where('symbol').equals(indexSymbol)
// .where('timestamp').gte(minDate)
.sort('timestamp')
.select('symbol price timestamp')
.exec(function (err, items) {
if(err) {
console.log("Error: %s", err);
} else {
console.log("There are %s items", items.length);
// There are 47425 items
}
});
我也一直在试图通过从查询检查我的架构调试此:
var query = Indexes
.find({'symbol':indexSymbol, 'timestamp': {$gte: minDate } })
.sort({'timestamp': 1});
console.log("Query = \n %s", util.inspect(query.model.schema));
它给出以下输出:
Query =
{ paths:
{ symbol:
{ enumValues: [],
regExp: null,
path: 'symbol',
instance: 'String',
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null },
price:
{ path: 'price',
instance: 'Number',
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null },
timestamp:
{ path: 'timestamp',
instance: undefined,
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null,
defaultValue: [Function: now] },
_id:
{ path: '_id',
instance: 'ObjectID',
validators: [],
setters: [Object],
getters: [],
options: [Object],
_index: null,
defaultValue: [Function: defaultId] },
__v:
{ path: '__v',
instance: 'Number',
validators: [],
setters: [],
getters: [],
options: [Object],
_index: null } },
subpaths: {},
virtuals: { id: { path: 'id', getters: [Object], setters: [], options: {} } },
nested: {},
inherits: {},
callQueue: [],
_indexes: [],
methods: {},
statics: {},
tree:
{ symbol: { type: [Function: String] },
price: { type: [Function: Number] },
timestamp: { default: [Function: now], type: [Function: Date] },
_id: { auto: true, type: [Function: ObjectId] },
id: { path: 'id', getters: [Object], setters: [], options: {} },
__v: [Function: Number] },
_requiredpaths: [],
discriminatorMapping: undefined,
_indexedpaths: undefined,
options:
{ id: true,
noVirtualId: false,
_id: true,
noId: false,
read: null,
shardKey: null,
autoIndex: true,
minimize: true,
discriminatorKey: '__t',
versionKey: '__v',
capped: false,
bufferCommands: true,
strict: true,
pluralization: true },
_events: {} }
个
问题:
1)我失去的东西,为什么我的查询将是MongoDB的外壳,但不是通过猫鼬的工作?
2)我的模式中'date'字段有什么问题吗?
在模式中,您将'timestamp'定义为'Date',但该字段是文档中的'string'。他们需要使用相同的类型。 – JohnnyHK
我很确定它实际上是一个'Date'值。 当我打电话了'typeof运算项[I] .timestamp'我得到 '对象',而不是'string' 指数 .find({}) .exec(函数(ERR,项目){ 如果(! err){ for(var i = 0; i
在shell中,Date值显示为ISODate(“2015-09-26T00:51:23.444Z”)'。您在阅读的文档中看到对象类型信息,因为Mongoose会将文档中的字段转换为模式中定义的类型。无论如何,这是我最好的猜测。 :) – JohnnyHK