当我尝试重塑我的集合中的日期字段时,我收到错误。
这是架构,我想重塑保存日期的sent
字段:
> db.complaints.findOne()
{
"_id" : ObjectId("55e5990d991312e2c9b266e3"),
"complaintID" : 1388734,
"product" : "mortgage",
"subProduct" : "conventional adjustable mortgage (arm)",
"issue" : "loan servicing, payments, escrow account",
"subIssue" : "",
"state" : "va",
"ZIP" : 22204,
"submitted" : "web",
"received" : "2015-05-22",
"sent" : "2015-05-22",
"company" : "green tree servicing, llc",
"response" : "closed with explanation",
"timely" : "yes",
"disputed" : ""
}
这是我收到的查询和错误消息:
db.complaints.aggregate([
{$project:
{_id:0,
sent: 1,
Year:{$year:"$sent"},
Month: {$month:"$sent"},
Day: {$dayOfMonth: "$sent"},
product : 1}},
])
> db.complaints.aggregate([
... {$project:
... {_id:0,
... sent: 1,
... Year:{$year:"$sent"},
... Month: {$month:"$sent"},
... Day: {$dayOfMonth: "$sent"},
... product : 1}},
...
... ])
assert: command failed: {
"errmsg" : "exception: can't convert from BSON type String to Date",
"code" : 16006,
"ok" : 0
} : aggregate failed
Error: command failed: {
"errmsg" : "exception: can't convert from BSON type String to Date",
"code" : 16006,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
at (shell):1:15
2015-09-12T13:16:22.876+0100 E QUERY Error: command failed: {
"errmsg" : "exception: can't convert from BSON type String to Date",
"code" : 16006,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
at (shell):1:15 at src/mongo/shell/assert.js:13
> db.complaints.aggregate([
... {$project:
... {_id:0,
... sent: 1,
... Year:{$year:"$sent"},
... Month: {$month:"$sent"},
... Day: {$dayOfMonth: "$sent"},
... product : 1}},
...
... ])
assert: command failed: {
"errmsg" : "exception: can't convert from BSON type String to Date",
"code" : 16006,
"ok" : 0
} : aggregate failed
Error: command failed: {
"errmsg" : "exception: can't convert from BSON type String to Date",
"code" : 16006,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
at (shell):1:15
2015-09-12T13:21:08.767+0100 E QUERY Error: command failed: {
"errmsg" : "exception: can't convert from BSON type String to Date",
"code" : 16006,
"ok" : 0
} : aggregate failed
at Error (<anonymous>)
at doassert (src/mongo/shell/assert.js:11:14)
at Function.assert.commandWorked (src/mongo/shell/assert.js:254:5)
at DBCollection.aggregate (src/mongo/shell/collection.js:1278:12)
at (shell):1:15 at src/mongo/shell/assert.js:13
>
答案 0 :(得分:3)
您只能将$year
之类的日期运算符用于具有BSON日期数据类型的值。您的sent
字段的值是一个字符串,因此您需要使用$substr
来按位置拆分日期字符串:
db.complaints.aggregate([
{$project:
{_id: 0,
sent: 1,
Year: {$substr: ["$sent", 0, 4]},
Month: {$substr: ["$sent", 5, 2]},
Day: {$substr: ["$sent", 8, 2]},
product: 1}}
])
结果:
{
"product" : "mortgage",
"sent" : "2015-05-22",
"Year" : "2015",
"Month" : "05",
"Day" : "22"
}