最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to fetch data from mongodb and convert it to json object in node.js? - Stack Overflow

programmeradmin2浏览0评论

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:

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.

What I want to do is something like this:

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

Please suggest. Thanks in advance!

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:

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.

What I want to do is something like this:

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

Please suggest. Thanks in advance!

Share Improve this question edited Jul 25, 2016 at 11:39 Dhanush Gopinath 5,7497 gold badges39 silver badges68 bronze badges asked Jul 25, 2016 at 11:22 varun kumar duttavarun kumar dutta 2021 gold badge4 silver badges10 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2

I remend you to use the toObject function.

cursor.each(function (err, doc) {

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

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.

use toArray in mongodb client , mongodb always give json format

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

aum.forEach(function (err, doc) {

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

        } else {
            callback();
        }
    });
})
发布评论

评论列表(0)

  1. 暂无评论