Why does the Node.JS community generally favor NoSQL over relational databases? - node.js

Why is MongoDB usually used in conjunction with NodeJS?, Is it just coincidental or are there good engineering reasons behind this combination?

There is no direct correlation between nodejs and mongo.
In particular mongo has drivers for the following languages:
C
C++
C#
Java
Node.js
Perl
PHP
Python
Ruby
Scala
The only correlation that I can find is that using nodejs queries are more similar to the same query written in the console of mongo than other languages (below an example in nodejs and in java).
In node js:
...
db.collection('restaurants').insertOne( {
"name":"Pizza Roma",
"city":"Rome",
"country":"Italy"
});
...
In java
...
Document restaurant = new Document("name", "Pizza Roma")
.append("city", "Rome")
.append("country", "Italy");
db.getCollection("restaurants").insertOne(document);
...

The structure of MongoDB documents is JSON-like.
JSON (JavaScript Object Notation) is syntactically identical to the code for creating JavaScript objects, so creating JSON structures from objects and parsing JSON to objects is really easy in JavaScript. You can directy insert a JavaScript object structure into MongoDB.
Other than that, MongoDB has Drivers for a vast array of languages.

There is no direct relation between Node.js and Mongodb. MongoDB is a
document based database which will give data as a JSON directly and it is schema less.To develop the rapid applications now a days MEAN stack apps are very famous.Actually Node.js is not limited to MongoDB it can be connected with different databases.

Related

Couchdb2 mango/find js api

I have noticed that all the couchdb api helpers don't implement Mango query
I have found cradle started working on it, but seems they stoped implementing, nothing on docs about it.
https://github.com/flatiron/cradle/blob/master/lib/cradle/database/mango.js
Is there a good js api that supports mango find?
Secondary: Is there a reason why no one seems to be implementing mango query, why is everyone sticking to map/reduce?
Based on the comments give, I would like to clarify my question:
I know about pouch-find, but I presume this is for local storage or local instance of pouch that could be found in browser or nodejs, but I want to find a library that I can use to query couchdb database on the server.
I have found a temporary solution for now. Im using cradle with query function
ex:
db.query({
method: 'POST',
path: "/_find",
body: {
selector:{"_id": "settings/12345" },
limit:1,
//use_index: "_all_docs"
}
}
So to further explain my setup. Im using one couchdb per user. That db will sync to browser using pouchdb, I can use pouch-find to query that synced local copy (Is this correct?).
But then I have other couch databases that are not synced, that can be accessed by many users. To query these databases I use cradle with the above example.
You can find pouchdb-find here which is in development. If there is no api helpers for Mango Query yet, it's probably because it`s new altough cloudant had this query language since a moment.

nodejs mongodb driver and GridFS

I'm using nodejs and the native mongodb driver v2.0.43 (latest as of 9/18/2015) and am having problems querying the db.fs.files collection. It is returning that fs is undefined yet I can use the mongo console and see the files that have been stored using db.fs.files.find().toArray(). Does anyone have any experience querying the db.fs.files collection via node using the mongodb driver?
thanks
Ben
Ok, after doing a better google query I found this solution: Can you use find queries on GridFS using javascript API?
The gist is you have to use 'fs.files' as the collection name.
db.collection('fs.files').find().toArray();

use Waterline as Standalone (no express)

Good Afternoon,
I am new with node.js and I try to develope an only command app.
For this app I need an ORM and I wish to use WATERLINE as standalone but not in express framework.
I looked at the example and I succeed to see my different collections.
// Our collections (i.e. models):
ontology.collections;
console.log(ontology.collections);
// Our connections (i.e. databases):
ontology.connections;
I am stucked after this. I can't find a way to return my models and make queries.
If someone could help me taht would be great.
Thanks
If you initialized Waterline in the ontology variable and got the collections successfully loaded as you say, now you can access each collection loaded (with loadCollection()) like this:
ontology.collection.mycollection
Where mycollection is the identity defined in your model.
Then you can make queries:
ontology.collection.mycollection.find(...)

Baffled by all this Node -> Titan stuff

I'm new to Java, Gremlin, Nodejs, Tickerpop, Maven and just about everything else. What does this code do? In particular what is 'java.import' doing? Is it a Java class? What has this got to do with Titan?
var Titan = require('titan-node');
var gremlin = new Titan.Gremlin({ loglevel: 'OFF' });
var TinkerGraphFactory = gremlin.java.import('com.tinkerpop.blueprints.impls.tg.TinkerGraphFactory');
var graph = TinkerGraphFactory.createTinkerGraphSync();
var g = gremlin.wrap(graph);
g.V('name', 'marko').next(function (err, v) {
v.getProperty('name', function (err, value) {
console.log(value);
});
});
Why when I use the Rexster can I not see the database being queried here?
To add to #mscdex valid answer.
This is JavaScript-flavored Gremlin code in Node.js using direct Java bindings via node-java.
Gremlin is not a language per se but a DSL. It is most of the time written in Groovy (because of its shortened syntax over Java), but it also exists in any JVM-compliant languages (ie. Java, Groovy, Scala, JavaScript via rhino and now nashorn with Java 8, to name a few). The full Groovy/Java API is accessible when typing Gremlin queries/scripts, which makes it a turing-complete "language".
I recommend reading http://gremlindocs.com/ and http://sql2gremlin.com for interesting beginner resources on Gremlin. http://www.tinkerpop.com/docs/3.0.0.M1/ will give you detailed information on TinkerPop and Gremlin (note: link will break as official v3.0 doc is released).
Because of the way node-java works and exposes Java methods (sync/async), you're required to use callbacks here in order to not block the event loop. This is a JavaScript concern and has nothing to do with Gremlin strictly speaking.
There a couple other clients which do not bind to the JVM directly but uses HTTP for TinkerPop 2.x (https://github.com/gulthor/grex for Node.js) or WebSocket for TinkerPop 3.0+ (https://github.com/gulthor/gremlin-client, for Node.JS/browsers, which will become the official TP3 JavaScript driver). Note: TinkerPop member / lib author here.
gremlin (a dependency of titan-node) uses node-java, which is a module that provides a bridge between node and Java. node-java allows you to import Java classes, instantiate Java data types, etc.
So what you're seeing is node-java importing a particular Java class because Gremlin is a Java/JVM thing.

Getting started with a database for Node

The most relevant question here on StackOverflow was this, which still doesn't answer my question because the answer nor the redis repo give a tutorial/walk-through that beginners don't understand.
The thing is, I have absolutely no idea how to setup a simple database in order to create a simple to-do list or blog on my own. This is probably the closest tutorial on how to setup a database. But it's lacking in a sense due to not having a schema or so defined in order for me to edit or add "tables".
Simply put, I'm looking for a tutorial a complete beginner is able to follow on how to setup a database and define custom schemas for data (e.g. products that can have reviews nested in them).
Any suggestions?
I guess this should cover it: Node.js, MongoDB and Mongoose
You mentioned that you wanted to create a Todo application. There is TodoMVC. You can see various MVC Frameworks in action. There's also an example with Mongoose and Backbone.
For installing different NoSQL options including MongoDB, CochDB, Redis and SQLite for use with Node.js this is a nice walk through.
Once installed the following steps are required to get going with the database. Typical example for mongodb:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/your_database');
var Schema = mongoose.Schema;
var User = new Schema({
'_id' : String,
'name' : String,
'votes' : Number });
var User_Model = mongoose.model('User', User);
Approaches to read/ write data from/to the database varies.
I think the simplest thing to do is to download and install MongoDB and use the mongodb-native driver to store and access your data.
MongoDB is schema-less so you won't need to define any table or keys in advance. Simply open a collection (which will be created automatically if it doesn't exist) and start storing documents/objects in it.
Mongo is fast, powerful and, in my opinion, easy to use.
See mongodb.org for more information.
I find this screencasts very helpful for nodeJS + MongoDB, even though the mongoose has been updated a lot since the video, but the basics remained. And you'll learn the new one in no time just by skimming mongoose's doc.
I've downloaded all of his videos and watch them every time I need brushing up my node skillz :p
Sidenote: He just uploaded screencast for couchDB, for other who prefers different cup of tea.

Resources