16

I am trying to connect AngularJS with MongoDB using Mongoose. I would like to pass the Models to be used by the Controllers, so I can $scope to the data. I am not sure if I have to go about setting up an Angular Service, if so, can you point me in the right direction. Thank You.

Overview:

Model:

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;

Controller:

// of course 'require' does not work but you get the idea
function OrderController($scope) {
  return $scope.orders = Order.find({});
}
1

2 Answers 2

20

You'll need an interim step there. Going directly from Angular to Mongo will not work out. If you want a generic REST interface to Mongo with which you can utilize Angular's bundled $http services, take a look at the list of REST services on Mongo's site.

Mongo REST Services

http://www.mongodb.org/display/DOCS/Http+Interface#HttpInterface-RESTInterfaces

Angular $http Service:

http://docs.angularjs.org/api/ng.$http

There's a lot of different options here, but this is likely the easiest way to get up and going.

Sign up to request clarification or add additional context in comments.

1 Comment

On the MongoDB page, they say to not use the REST API in production. Anyone any opinions to this? It looks perfect for this usecase, but I am still having doubts using it because of this statement... (Reference: docs.mongodb.org/manual/core/security-interface/#rest-api )
10

The interim step has been provided by a npm module, angoose. It does exactly what you asked for: making the mongoose models available as Angular injectable factories. All you need to do is just including a script tag in your html file.

angoose module:

https://npmjs.org/package/angoose

Disclaimer: I'm the contributor of angoose module.

3 Comments

First I thought of editing this answer from angoose to mongoose.
Well aren't you angoose.
is it forced to have express? Im not familiar with it and it look confusing. Although using angoose seems to be what Im looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.