Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - Interaction with mongodb

I am new to MEAN stack, I am trying to create a basic one page application at the moment.

I am trying to connect to the mongodb and then list the values in a certain collection in a controller.

However, when I looked for the answer, I came across this answer Using AngularJs and MongoDB/Mongoose

Which then confuses me as what is the point of having the code below if you can't use it between angular and mongo ? Or are there other interim steps that use it?

var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://localhost:3000/database');
var orderSchema = new mongoose.Schema({
    routeFrom : String,
    routeTo : String,
    leaving: String
});
var Order = db.model('Order', orderSchema);
module.exports = Order;

Edit: The situation i am trying to use it in is such:

Geek.html

<div class="jumbotron text-center">
    <h1>Geek City</h1>

    <p>{{tagline}}</p>

    <ul>
        <li ng-repeat="value in dataValues">
            {{value.name}}
        </li>
    </ul>


</div>

GeekController

angular.module('GeekCtrl', []).controller('GeekController', function($scope) {

    $scope.tagline = 'The square root of life is pi!';

    $scope.dataValues = function(){

        var mongo = require('../config/db.js');

        var collectionValues = mongo.myCollection.find();

        return collectionValues;
    };
});
like image 256
user2469515 Avatar asked May 03 '14 01:05

user2469515


People also ask

Can we connect angular with MongoDB?

We cannot connect angular to the database directly without sending an http request to the node. We would have our angular app and our MongoDB database on a server. We would directly send queries to the database from our angular app and surpass our node express application.

Can AngularJS connect to database?

AngularJS is perfect for displaying data from a Database. Just make sure the data is in JSON format.

Can JavaScript connect to MongoDB?

The MongoDB JavaScript (NodeJS) Driver makes it simple to work with MongoDB databases from Node. js applications. To connect to your database and run the queries discussed in this Quick Start series, you'll need the MongoDB JavaScript (NodeJS) driver.


2 Answers

You cannot require db.js config file in Angular because it's not set to be used on the client side. What you describe is so called 'Isomorphic' approach.

What I mean by that: mongo is a database system (roughly speaking). To get data from the database, we usually don't trust the client. So we have some server-side code (PHP, Ruby, Node.js, Java, what have you) which authorizes the client, processes and filters the data and returns it to the client (in this case Angular.js). So your Mongoose models are set to be used by the server-side javascript and that part of the app. That server side should also serve data to Angular so you'd connect to Node.js from Angular, not directly to Mongo. So the same server that (usually) serves your angular files, will also serve the data it reads from mongo.

If you want server-less data with Angular, you can take a look at Firebase.js. It's angular-ready and it could help you not mess around with Mongo, mongoose and the server-side code.

You could try a hybrid approach with something like meteor.js or backbone.js set to work both on client and server, or take a look at this article for more info.

Or for what it's worth, if you want to run your own Mongo, you could start mongo with --rest, then you'd be able to connect to Angular directly to Mongo, at http://somehost:28017/mydatabase or something similar, depending on your setup.

like image 102
Zlatko Avatar answered Sep 27 '22 21:09

Zlatko


Mongoose is a node module, and as far as I know it doesn't have a front end component, so you won't be using it directly in your frontend js code. It's only going to help you on the server side of your app. If you're relatively new to Node then this stuff can get pretty confusing, since it's all end-to-end javascript and sometimes it's not clear what modules work on the server or frontend, especially since some can do both.

Node, MongoDB, Express, and Mongoose all live on the server.

Angular lives in the browser, and can't use any of the server-side components directly.

Using the MEAN stack, You will be building a node app that uses mongoose to talk to mongodb and express to expose an api to your front end. Then in in your html/js code you'll be using angular and its $http service to talk to the server to get and set data.

There is a great tutorial that walks you through the entire process on scotch.io: http://scotch.io/bar-talk/setting-up-a-mean-stack-single-page-application

like image 28
Matt Guest Avatar answered Sep 27 '22 21:09

Matt Guest