在 MongoDB 中,我们使用 find 查询记录时,返回的游标结果,我们可以使用 printjson 将其打印出来,并且,打印出来的是 json 格式的数据。
printjson(course)
变量 | 描述 |
---|---|
course | 游标 |
在使用 printjson 输出游标时,会直接输出 json 格式。
我们首先,使用 mongo 命令,连接上数据库,具体命令如下:
mongo
如下图所示:
现在,我们使用 use 命令,切换到 haicoder 数据库,具体命令如下:
use haicoder
现在,我们使用 insert 插入四条记录,具体命令如下:
db.haicoder.insert([ {id:1, "url" : "haicoder.net/c", "score" : 100 }, {id:2, "url" : "haicoder.net/cpp", "score" : 90 }, {id:3, "url" : "haicoder.net/golang", "score" : 80 }, {id:4, "url" : "haicoder.net/java", "score" : 60 } ]);
执行完毕后,此时,如下图所示:
我们看到,此时提示我们成功插入了多条记录,现在,我们使用 find 查询,具体命令如下:
var course = db.haicoder.find({}, {_id:0});
执行完毕后,我们使用 printjson 打印游标数据,具体命令如下:
printjson(course.next());
此时,如下图所示:
我们看到,我们使用 printjson 输出了游标的 json 数据。
在 MongoDB 中,我们使用 find 查询记录时,返回的游标结果,我们可以使用 printjson 将其打印出来,并且,打印出来的是 json 格式的数据。