Mongodb日期范围查询返回无结果
问题描述:
我对MongoChef中的下列查询没有任何兴趣 - 它不返回任何结果。如果我在mLab中运行相同的查询,那么它可以工作。我想知道它有什么问题。Mongodb日期范围查询返回无结果
$match : {
"account_balances_date": {
"$gte": {
"$date": "2016-04-01T00:00:00.000Z"
},
"$lte": {
"$date": "2016-08-01T00:00:00.000Z"
}
}
}
下面是查询范围内的account_balances_date
示例文档。感谢
{
"_id": {
"$oid": "5799ba3ff36d280883999c06"
},
"object_class": "Account Balances",
"object_category": "Application",
"object_type": "Report on Balances",
"object_origin": "Sage One",
"object_origin_category": "Bookkeeping",
"object_creation_date": {
"$date": "2016-05-15T22:49:35.665Z"
},
"party_uuid": "phildominickcompany",
"connection_uuid": "5738fc661a21db15b5c45b49",
"account_balances_date": {
"$date": "2016-04-30T10:00:00.000Z"
},
"account_balances": [
{
"account_identifier": "1100",
"account_name": "Trade Debtors",
"account_category": "Current Assets",
"account_type": "Debtors",
"account_currency": null,
"account_value": 103800,
"account_value_type": "debit"
},
{
"account_identifier": "2100",
"account_name": "Trade Creditors",
"account_category": "Current Assets",
"account_type": "Creditors",
"account_currency": null,
"account_value": 53700,
"account_value_type": "credit"
},
{
"account_identifier": "2200",
"account_name": "VAT on Sales",
"account_category": "Current Liabilities",
"account_type": "Tax",
"account_currency": null,
"account_value": 17300.01,
"account_value_type": "credit"
},
{
"account_identifier": "2201",
"account_name": "VAT on Purchases",
"account_category": "Current Liabilities",
"account_type": "Tax",
"account_currency": null,
"account_value": 8950,
"account_value_type": "debit"
},
{
"account_identifier": "3260",
"account_name": "Drawings - equity",
"account_category": "Equity",
"account_type": null,
"account_currency": null,
"account_value": 65000,
"account_value_type": "debit"
},
{
"account_identifier": "4000",
"account_name": "Sales Type A",
"account_category": "Revenue",
"account_type": "Sales",
"account_currency": null,
"account_value": 16666.67,
"account_value_type": "credit"
},
{
"account_identifier": "5000",
"account_name": "Cost of sales - goods",
"account_category": "Expense",
"account_type": "Sales",
"account_currency": null,
"account_value": 2500,
"account_value_type": "debit"
},
{
"account_identifier": "5010",
"account_name": "Cost of sales - materials",
"account_category": "Expense",
"account_type": "Sales",
"account_currency": null,
"account_value": 10000,
"account_value_type": "debit"
}
],
"debtors_and_creditors": []
}
答
如上所述here,操作者$date
是扩展JSON
格式存储date
对象。在运行查询时,所有GUI工具可能都无法识别。相反,你可能想用ISODate("2016-04-01T00:00:00.000Z")
来代替:
$match: {
"account_balances_date": {
"$gte": ISODate("2016-04-01T00:00:00.000Z"),
"$lte": ISODate("2016-08-01T00:00:00.000Z")
}
}
真棒@yaoxing谢谢你,完美 –