Frontend accessing MongoDb - javascript

For a project, we're going to use the MEAN stack. Having Angularjs as the frontend framework, is there a possibility for the framework directly accessing the data from mongodb (Bypassing node and express)?
Also, is it possible to use meteorjs on the client side? If ever, what are key advantages and can it do direct access to mongodb as well?

Frontend accesing MongoDB is possible, via its HTTP (rest) interface
http://docs.mongodb.org/ecosystem/tools/http-interfaces/
To get the contents of a collection (note the trailing slash):
http://127.0.0.1:28017/databaseName/collectionName/
To add a limit:
http://127.0.0.1:28017/databaseName/collectionName/?limit=-10
To skip:
http://127.0.0.1:28017/databaseName/collectionName/?skip=5
To query for {a : 1}:
http://127.0.0.1:28017/databaseName/collectionName/?filter_a=1
Separate conditions with an &:
http://127.0.0.1:28017/databaseName/collectionName/?filter_a=1&limit=-10
Same as db.$cmd.findOne({listDatabase:1}) on the admin database in the shell:
http://localhost:28017/admin/$cmd/?filter_listDatabases=1&limit=1
To count documents in a collection:
http://host:port/db/$cmd/?filter_count=collection&limit=1
However, I personally discourage this approach. Node/Express can be a simple wrapper for auth/auth before you make any changes to DB.

Related

How to filter and select specific fields from mongodb in a GET request?

I have just started working with mongoose/mongodb and node.js to build the backend of a react native app I am working on. I'm getting fairly comfortable with querying documents in the database from node but how do I set those "filters" in a GET request (using fetch) from the front end?
If a user applies a filter to a list to only return products of a certain price, how do I get that filter from the client side over to the server side so node can make the proper query on the database?
Same question for if I only want to return specific fields as opposed to returning every field in a mongodb document. How can I let the backend know what fields to return from the client side?
Hopefully that all makes sense, like I said I'm very new to both mongo and node so I apologize if my explanations are not the best.
Thank you to anyone who can help!
What you're looking for is a query language - a syntax for conveying the parameters of a query to a server. There are several options.
Some sites decide to only support simple queries. The simplest query language, in this case, is the GET query string. For instance, if you're building an on-line store, your customers will typically need to search for products by name and by category, and so you may end up with two query string parameters that you support in URLs, like this:
/products?category=clothes&search=t-shirt
Then, your application must process each of these parameters independently - your back-end must know how to turn this into a MongoDB query. For example:
products.find({
category: { $eq: req.query.category },
$text: { $search: req.query.search }
// similarly, you could accept params such as "pricemin" and "pricemax"
// and construct a { $gt, $lt } query
});
For more complex systems or applications that require more flexibility, you can allow the client to construct a complete query on their own and pass that to the back-end for evaluation. Multiple solutions exist that let you achieve this - GraphQL is a popular choice lately due to its good tooling support and ease of use. Another well-known solution is OData, which is used in some high-profile APIs, such as Microsoft Graph API.
If using an advanced query language, your back-end will always need to translate between the format that's sent to the server and the format that the database speaks. The simplest, non-generic example is the code snippet above - though batteries-included solutions will normally come with their own translators, so you may not need to write any code yourself if a package exists that does that for you.

How to use Parse server without MongoDb?

I only need to use Parse sever cloud functions, How can I run parse server without mongodB
How to use Parse server without MongoDb ?
You cannot run a Parse Server instance without a database. For example, Parse store login session into the database
Two databases available with Parse Server
According to the documentation you have two choices of databases:
Postgres
MongoDb
So if you do not want to use MongoDb you have to use Postgres
Here is the difference between theses two databases
How to switch databases ?
If you want to change your database from a MongoDb to a Postgres, you just have to change the configuration:
var api = new ParseServer({
databaseURI: 'postgres://my.awesome.postgres.uri',
});
Hope my answer help you 😊

How I can insert to MongoDB using Rest API with Javascript?

I am trying to insert the record into my mongoDB. Here is what I tried:
$.ajax({
type: "POST",
url: "http://localhost:28017/messages_sent/better/?doc=",
data: {
thread_id: event.threadID,
message_id: event.messageID,
user_email: sdk.User.getEmailAddress(),
token: token
},
dataType: "JSON"
});
countUsage();
chrome.storage.sync.set({ "last_tracked_email_date": Date.now() });
alert("hello");
}
});
I am trying to look into the MongoDB, but could not find any thing in the database.
How I can insert the record into MongoDB using javascript and Rest API of MongoDB itself?
After a good discussion, I get to know that you were trying, is to just insert data in Mongo.
And this Mongo's rest API feature only supports retrieving data
from official docs
The mongod process includes a simple REST API as a convenience. With no support for insert, update, or remove operations, it is generally used for monitoring, alert scripts, and administrative tasks.
So, there are many ways you can insert data in Mongo:
Using APIs in server side code using any server side lang like Python/java/node
Using mongo shell, simple insert
using some GUI tool, like robomongo
and I'm sure there are more
But in comments, OP wanted some data in Mongo to test functionality, so writing APIs for just inserting is not a great solution.
So, if you just want to insert data and comfortable using CLI, then just go in mongo shell, and insert directly into the collection.
But if not, then Robomongo is very efficient. It's made for adding/removing and other CRUD operations.
Simple steps for using robomongo:
Download from official site and install
Open it and click on connect, add your port (default 28017) and host (localhost)
Now on lhs, click on MongoDB and you should see you dbs in it it, if you've created those.
Then you can see your collections inside it, and just right click and click on insert option and you can add your JSON into it and click save.
That's it, doc is saved in collection. Simple right.

Expose GraphQL schema from non-JS server to a JS client

I have a GraphQL server implemented in Java and a JavaScript client querying it. What I don't like is that the client has to just know the schema and can not get it from the server instead and dynamically build queries against it.
Now, I understand GraphiQL somehow does just that, but I'm guessing it's because its backend is also written in JavaScript so both the client and server can use it. My schema is defined in Java, but there might be a way to automatically generate a JavaScript representation that the client could use.
Does such a thing already exist?
Now, I understand GraphiQL somehow does just that, but I'm guessing it's because its backend is also written in JavaScript so both the client and server can use it.
Actually, (fortunately) this is not the case. It is written in Javascript, but it need not be to achieve this behavior.
I've got some great news for you...
Introspection!
One of the awesome things about GraphQL is that, in fact, the client doesn't have to know anything about the schema, because it can just query the server for it using introspection. In fact, GraphiQL will use this automatically if you don't provide a schema explicitly to automagically populate it.
From the Props section of the GraphiQL README:
schema: a GraphQLSchema instance or null if one is not to be used. If undefined is provided, GraphiQL will send an introspection query using the fetcher to produce a schema.
The official GraphQL Introspection docs will give you lot more information and sample queries. Their example of querying their Star Wars example schema:
{
__schema {
types {
name
}
}
}
This returns the names of all of the types. Introspection is part of the GraphQL spec, so every GraphQL server should be able to do it out of the box: you don't need to explicitly add any functionality.

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;
};
});
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.
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

Categories

Resources