简体   繁体   English

如何使用mongodb javascript本机驱动程序访问mongodb find()操作的结果

[英]How to access the results of mongodb find() operation using mongodb javascript native driver

I have a database in mongodb and I'm connecting to it through NodeJS and mongodb native driver. 我在mongodb中有一个数据库,并且正在通过NodeJS和mongodb本机驱动程序连接到该数据库。 I'm trying to write a javascript code to print in the browser the information retrieved from the database through db.foo.find() operation. 我正在尝试编写一个JavaScript代码,以在浏览器中打印通过db.foo.find()操作从数据库检索到的信息。

I am currently able to print the value part of the json data. 我目前能够打印json数据的值部分。 However, i need to print the key part as well. 但是,我也需要打印关键部分。

Example : json data { key1:v1, key2:v2 ....} i need a way to access the key1 and key2 values through javascript code without knowing them prehand?? 示例:json数据{key1:v1,key2:v2 ....}我需要一种通过javascript代码访问key1和key2值而无需事先知道的方法吗?

您可以使用for ... in语法来迭代对象的键。

Object.keys(json).forEach(function (key) {
    console.log(key);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM