如何从mongodb获取数据并将其转换为node.js中的json对象?

时间:2022-12-04 15:37:58

I want to fetch the data from mongo db and store it in a json object in node.js. The purpose is to manipulate that data which I think is quite simple if its a json objcet. Below is the code I am using:

我想从mongo db获取数据并将其存储在node.js中的json对象中。目的是操纵那些我认为非常简单的数据,如果它是一个json objcet。以下是我使用的代码:

var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var url = 'mongodb://localhost:27017/MyCollection';

var data;

var findRestaurants = function (db, callback) {
    var cursor = db.collection('demographicdetails').find().limit(10);

    cursor.each(function (err, doc) {

        if (doc != null) {
            console.dir(doc);

        } else {
            callback();
        }
    });
};

MongoClient.connect(url, function (err, db) {

    findRestaurants(db, function () {
        db.close();
    });
});

From above code, I am able to fetch the data from mongo db and console.dir(doc) shows me the data.

从上面的代码,我能够从mongo db获取数据,console.dir(doc)向我显示数据。

What I want to do is something like this:

我想做的是这样的:

  data = doc;
 data.forEach(function (eval) {
     //Manipulating eval
 });

Please suggest. Thanks in advance!

请建议。提前致谢!

3 个解决方案

#1


1  

MongoDB uses JSON/BSON internally to store the data. So any query you make you should get a JSON object. I use MongooseJS as the MongoDB library, which gives you back a JSON after querying.

MongoDB在内部使用JSON / BSON来存储数据。所以你做的任何查询都应该得到一个JSON对象。我使用MongooseJS作为MongoDB库,它在查询后返回给你一个JSON。

#2


0  

I recommend you to use the toObject function.

我建议你使用toObject函数。

cursor.each(function (err, doc) {

    if (doc != null) {
        console.dir(doc);
        var restaurant = doc.toObject(); // use restaurant object
    } else {
        callback();
    }
});

#3


0  

use toArray in mongodb client , mongodb always give json format

在mongodb客户端使用toArray,mongodb总是给json格式

db.collection('demographicdetails').find().limit(10).toArray(function (err, aum) { 

aum.forEach(function (err, doc) {

        if (doc != null) {
            console.dir(doc);

        } else {
            callback();
        }
    });
})

#1


1  

MongoDB uses JSON/BSON internally to store the data. So any query you make you should get a JSON object. I use MongooseJS as the MongoDB library, which gives you back a JSON after querying.

MongoDB在内部使用JSON / BSON来存储数据。所以你做的任何查询都应该得到一个JSON对象。我使用MongooseJS作为MongoDB库,它在查询后返回给你一个JSON。

#2


0  

I recommend you to use the toObject function.

我建议你使用toObject函数。

cursor.each(function (err, doc) {

    if (doc != null) {
        console.dir(doc);
        var restaurant = doc.toObject(); // use restaurant object
    } else {
        callback();
    }
});

#3


0  

use toArray in mongodb client , mongodb always give json format

在mongodb客户端使用toArray,mongodb总是给json格式

db.collection('demographicdetails').find().limit(10).toArray(function (err, aum) { 

aum.forEach(function (err, doc) {

        if (doc != null) {
            console.dir(doc);

        } else {
            callback();
        }
    });
})