Pymongo-遍历所有文档和每个文档的新字段

时间:2018-11-03 03:28:03

标签: python mongodb pymongo

我的文档集中具有以下结构:

{
"_id" : ObjectId("5af599c632214d9410ada4c4"),
"platform" : "Web",
"sessionId" : "fd91b2c3-1615-4dfd-9fa4-b8aa6fa95747",
"timestamp" : NumberLong(1526045124702),
"chatDate" : "11-05-2018",
"hour" : 18,
"month" : 5,
"time" : "18:55:24",
"weekDay" : "Monday"
}  

我想遍历所有这些文档的集合,单独获取时间戳字段,从时间戳获取日期,小时,月份,时间和工作日,并向文档添加新字段,如下所示:

[11/03/18 10:55:02] [SSH] Opening SSH connection to <my IP>.
[11/03/18 10:55:04] [SSH] WARNING: SSH Host Keys are not being verified. Man-in-the-middle attacks may be possible against this connection.
[11/03/18 10:55:05] [SSH] authentication successful.
[11/03/18 10:55:06] [SSH] The remote user's environment is:
BASH=/bin/bash
BASHOPTS=cmdhist:complete_fullquote:extquote:force_fignore:hostcomplete:interactive_comments:progcomp:promptvars:sourcepath
BASH_ALIASES=()
BASH_ARGC=()
BASH_ARGV=()
BASH_CMDS=()
BASH_EXECUTION_STRING=set
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="4" [1]="3" [2]="48" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")
BASH_VERSION='4.3.48(1)-release'
DIRSTACK=()
EUID=0
GROUPS=()
HOME=/root
HOSTNAME=ubuntu-s-1vcpu-1gb-sfo2-01
HOSTTYPE=x86_64
IFS=$' \t\n'
LANG=en_US.UTF-8
LOGNAME=root
MACHTYPE=x86_64-pc-linux-gnu
MAIL=/var/mail/root
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
PIPESTATUS=([0]="0")
PPID=12950
PS4='+ '
PWD=/root
SHELL=/bin/bash
SHELLOPTS=braceexpand:hashall:interactive-comments
SHLVL=1
SSH_CLIENT='115.156.206.6 46676 22'
SSH_CONNECTION='115.156.206.6 46676 178.128.184.142 22'
TERM=dumb
UID=0
USER=root
XDG_RUNTIME_DIR=/run/user/0
XDG_SESSION_ID=465
_=']'
[11/03/18 10:55:07] [SSH] Starting sftp client.
[11/03/18 10:55:09] [SSH] 正在拷贝最新版本的 remoting.jar...
java.io.IOException: Could not copy remoting.jar into '/root/jenkins' on agent
at hudson.plugins.sshslaves.SSHLauncher.copyAgentJar(SSHLauncher.java:1110)
at hudson.plugins.sshslaves.SSHLauncher.access$400(SSHLauncher.java:128)
at hudson.plugins.sshslaves.SSHLauncher$2.call(SSHLauncher.java:864)
at hudson.plugins.sshslaves.SSHLauncher$2.call(SSHLauncher.java:831)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.io.IOException: Could not copy remoting.jar to '/root/jenkins/remoting.jar' on agent
at hudson.plugins.sshslaves.SSHLauncher.copyAgentJar(SSHLauncher.java:1105)
... 7 more
Caused by: java.io.FileNotFoundException: remoting.jar
at hudson.model.Slave$JnlpJar.getURL(Slave.java:429)
at hudson.model.Slave$JnlpJar.connect(Slave.java:393)
at hudson.model.Slave$JnlpJar.readFully(Slave.java:458)
at hudson.plugins.sshslaves.SSHLauncher.copyAgentJar(SSHLauncher.java:1094)
... 7 more
[11/03/18 10:55:09] Launch failed - cleaning up connection
[11/03/18 10:55:09] [SSH] 连接关闭。

我看到了this的答案,在这里我可以使用forEach遍历文档和更新。但是在pymongo中,似乎没有forEach功能。

如何在 python pymongo 中编写代码以实现此目标?

0 个答案:

没有答案