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.
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.
stringify() to return JSON data): We will now use http. createServer() and JSON. stringify() to return JSON data from our server.
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.
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.
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).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With