Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB将日期转换为字符串_Mongodb - Fatal编程技术网

MongoDB将日期转换为字符串

MongoDB将日期转换为字符串,mongodb,Mongodb,问题: { ... "SentTimestamp" : ISODate("2015-12-23T22:20:15Z") ... } db.foo.find({}, {$dateToString: {format:"%Y-%m-%d", date:"$SentTimestamp"}}) Error: error: { "$err" : "Can't canonicalize query: BadValue >1 field in obj: { format: \"%Y-%m-%d

问题:

{
...
"SentTimestamp" : ISODate("2015-12-23T22:20:15Z")
...
}
db.foo.find({}, {$dateToString: {format:"%Y-%m-%d", date:"$SentTimestamp"}})
Error: error: {
    "$err" : "Can't canonicalize query: BadValue >1 field in obj: { format: \"%Y-%m-%d\", date: \"$SentTimestamp\" }",
    "code" : 17287
从“SentTimestamp”获取Y-m-d时出错:ISODate(“2015-12-23T22:20:15Z”)

详细信息:

{
...
"SentTimestamp" : ISODate("2015-12-23T22:20:15Z")
...
}
db.foo.find({}, {$dateToString: {format:"%Y-%m-%d", date:"$SentTimestamp"}})
Error: error: {
    "$err" : "Can't canonicalize query: BadValue >1 field in obj: { format: \"%Y-%m-%d\", date: \"$SentTimestamp\" }",
    "code" : 17287
文档:

{
...
"SentTimestamp" : ISODate("2015-12-23T22:20:15Z")
...
}
db.foo.find({}, {$dateToString: {format:"%Y-%m-%d", date:"$SentTimestamp"}})
Error: error: {
    "$err" : "Can't canonicalize query: BadValue >1 field in obj: { format: \"%Y-%m-%d\", date: \"$SentTimestamp\" }",
    "code" : 17287
查询:

{
...
"SentTimestamp" : ISODate("2015-12-23T22:20:15Z")
...
}
db.foo.find({}, {$dateToString: {format:"%Y-%m-%d", date:"$SentTimestamp"}})
Error: error: {
    "$err" : "Can't canonicalize query: BadValue >1 field in obj: { format: \"%Y-%m-%d\", date: \"$SentTimestamp\" }",
    "code" : 17287
错误:

{
...
"SentTimestamp" : ISODate("2015-12-23T22:20:15Z")
...
}
db.foo.find({}, {$dateToString: {format:"%Y-%m-%d", date:"$SentTimestamp"}})
Error: error: {
    "$err" : "Can't canonicalize query: BadValue >1 field in obj: { format: \"%Y-%m-%d\", date: \"$SentTimestamp\" }",
    "code" : 17287
有人能解释一下如何将日期转换为字符串吗?上面有什么错误?

在方法中不能使用带投影的运算符。相反,在“或管道”阶段将其与一起使用,以返回将datetime字段转换为所需格式的字符串的文档,如以下示例所示:

使用:

或使用


@chridam-我们可以混合使用find和aggregate吗。。我还必须使用find查询。。这可能吗?是的,您可以使用管道步骤在聚合操作中包含查询。阅读更多示例。在没有
$project
的情况下是否有机会做到这一点?因为我不想从一个条目中投影所有字段。它们大约是40个,这是一个很大的数目。@Tenzolinho您可以像在上面的编辑中一样使用