Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return JSON from MongoDB in Node.js?

I have a mongodb database called pokemon with a collection called pokemons. Here is my attempt to write a function that will do a find() operation in mongodb:

'use strict';

var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');

// db url
var url = 'mongodb://localhost:27017/pokemon';

exports.getPokemonByName = function (name) {

  MongoClient.connect(url, function(err, db) {
    assert.equal(null, err);
    var cursor = db.collection('pokemons').find({name: name});

    // how to return json? 
  });

};

I then call this function in another file:

var express = require('express');
var router = express.Router();

router.get('/pokedex', function (req, res) {
  res.jsonp(db.getPokemonByName('Dratini'));
})

This link is helpful in showing how to log mongodb data to the console by doing some sort of each() method on the cursor object, but I don't know how to return json through the getPokemonByName function. I tried to define an empty array on the root scope of the getPokemonByName function and push data into that array with each iteration of the .each method show in that link, but I think I still can't return that array because it happens after the fact.

BTW, I'm really just doing this for fun and to learn about MongoDB and Node.js, so I don't want to use or an ODM like Mongoose to do some of this work for me.

like image 823
just another profile name Avatar asked Nov 02 '15 03:11

just another profile name


People also ask

How get JSON data from MongoDB?

Format Option: Choose JSON-mongo shell/ JSON-mongoexport. Target: Choose between clipboard/file & make sure the file path is defined. Others: Choose whether to export with commas between documents or export as a document array. Output Preview: Displays the final JSON document.

How do I return JSON data in node JS?

stringify() to return JSON data): We will now use http. createServer() and JSON. stringify() to return JSON data from our server.

How do I find data in MongoDB node Node JS?

Node.js MongoDB Find. In MongoDB we use the find and findOne methods to find data in a collection. Just like the SELECT statement is used to find data in a table in a MySQL database.

What is mongoose in Node JS?

Mongoose is a promise-based Object Data Modeling (ODM) library for the Node.js framework. Mongoose simplifies how you interact with a MongoDB database. It allows you to create and model MongoDB schema. This way, you avoid complex writing of database queries/schemas. Mongoose gives you the ability to model the data you want to store in MongoDB.

How to return JSON data in our application from NodeJS?

The following ways cover how to return JSON data in our application from Node.js. Method 1 (Using Express.js): Express is a back end web application framework for Node.js. It is one of the standard frameworks which is used by many developers. To install it, we will be using NPM (Node Package Manager).

Does MongoDB use JSON data types?

No. MongoDB uses BSON (Binary JSON), which has been extended to add some optional non-JSON-native data types such as dates and binary data. JSON is converted to BSON to be stored in MongoDB and converted back to JSON when retrieved from the database.


1 Answers

Found a simple tweak for this. Let say the callback to the findOne returns result then you can convert the result to JSON object like this

result = JSON.parse(JSON.stringify(result))

Now you can access the result and its fields simply with the dot operator.

like image 199
Deepika Sharma Avatar answered Oct 10 '22 17:10

Deepika Sharma