What is the optimal way to perform CRUD operations when using Node.js with MongoDB. Can I reuse the same queries that work on mongo shell? What advantages does a ODM like mongoose provide? Any other ODMs which fit into the mean.io stack?
vmr.
Well, I guess it depends of what you want to do.
Lets take what Mongoose says in their website:
Mongoose provides a straight-forward, schema-based solution to modeling your application data and includes built-in type casting, validation, query building, business logic hooks and more, out of the box.
Resuming what I understood of that it helps you to model your database and helps you to mantain your logic organized using model like in an MVC. It's a very mature ODM and very recomended for using with MVC.
In my personal experience I started using Monk, which did the trick for a while, but the I started to need to use aggregate and other stuff that apparently Monk can't handle. And I don't wanted to tie my system to an model because is a very mutable project, so I started using Mongoskin which is, at least for now, perfect for me because I can use pratically the same query that I use at Robomongo (Which is like a Navicat, PgAdmin, PhpMyAdmin but for MongoDB) in my ExpressJs code.
Mongoose is saving your time by mapping the JavaScript Data objects to the MongoDB database.
MongoDB and NodeJS Integration
Related
I just want to know, what are differences of mongoose and mongoJS. And, what the advantage and the lack if we use mongoose or mongoJS ?
Because we know that are many depedency that use to connecting NodeJS and MongoDB. For example mongoose and mongoJS.
Mongoose is higher level interface to Mongodb and actually uses mongodb.js, the MongoDB driver. The question is not really which one is better or worse, the question for us is
> Do the benefits of an ODM in Mongoose outweigh the drawbacks?
If you’re looking for an object modeling (ODM) tool so that you do not have to learn a lot about the way Mongodb works, then Mongoose is probably for you. If you want a fast driver and really get the most out of Mongodb, then use the native driver. We know our way around Mongodb so Mongoose would have slowed use down and our app.
If you are attempting to connect to a MongoDB database instance, then you will need some sort of driver. I have never heard of a MongoJS, but maybe you are referring to the MongoDB driver? This driver will help you communicate with the database, such as saving documents or retrieving them.
MongooseJS is an object modeling library. It sits on top of the MongoDB driver and manages relationships and object mapping. It can detect changed properties in objects, and then run validation and update operations.
You don't need to explicitly install both, as Mongoose will include the MongoDB driver when installing through NPM. You don't need either if you are not connecting to a MongoDB database.
Better you prefer mongoose since it comes with both libraries which may used to invoke instances and drivers for mongoDB connection .
mongoose is ORM - Object-relational mapping. It adds an abstract layer with many methods to manipulate data. Such as chaining like this:
Tank
.find({ size: 'small' })
.where('createdDate')
.gt(oneYearAgo)
.exec(callback);
It may be good for newbies because it's easy to use, with clear syntax. But it may be slower than native approach.
MongoJS is just a wrapper of node-mongodb-native, with some improvements. What is native? It's a driver that allows to directly call to MongoDB from Node.js program. While native version by MongoDB is the best choice for performance, it follows origin syntax same as Mongo Shell, that's quite complicate for newcomers.
Personally, I used Mongoose at first. Now I like to use node-mongodb-native, but have to add some methods to make it easier. Anyway, MongoJS is a balance solution.
I just want to know, what are differences of mongoose and mongoJS. And, what the advantage and the lack if we use mongoose or mongoJS ?
Because we know that are many depedency that use to connecting NodeJS and MongoDB. For example mongoose and mongoJS.
Mongoose is higher level interface to Mongodb and actually uses mongodb.js, the MongoDB driver. The question is not really which one is better or worse, the question for us is
> Do the benefits of an ODM in Mongoose outweigh the drawbacks?
If you’re looking for an object modeling (ODM) tool so that you do not have to learn a lot about the way Mongodb works, then Mongoose is probably for you. If you want a fast driver and really get the most out of Mongodb, then use the native driver. We know our way around Mongodb so Mongoose would have slowed use down and our app.
If you are attempting to connect to a MongoDB database instance, then you will need some sort of driver. I have never heard of a MongoJS, but maybe you are referring to the MongoDB driver? This driver will help you communicate with the database, such as saving documents or retrieving them.
MongooseJS is an object modeling library. It sits on top of the MongoDB driver and manages relationships and object mapping. It can detect changed properties in objects, and then run validation and update operations.
You don't need to explicitly install both, as Mongoose will include the MongoDB driver when installing through NPM. You don't need either if you are not connecting to a MongoDB database.
Better you prefer mongoose since it comes with both libraries which may used to invoke instances and drivers for mongoDB connection .
mongoose is ORM - Object-relational mapping. It adds an abstract layer with many methods to manipulate data. Such as chaining like this:
Tank
.find({ size: 'small' })
.where('createdDate')
.gt(oneYearAgo)
.exec(callback);
It may be good for newbies because it's easy to use, with clear syntax. But it may be slower than native approach.
MongoJS is just a wrapper of node-mongodb-native, with some improvements. What is native? It's a driver that allows to directly call to MongoDB from Node.js program. While native version by MongoDB is the best choice for performance, it follows origin syntax same as Mongo Shell, that's quite complicate for newcomers.
Personally, I used Mongoose at first. Now I like to use node-mongodb-native, but have to add some methods to make it easier. Anyway, MongoJS is a balance solution.
I have a one file which has details of the database named xyz.js and another file which created the database using that file in mongodb and also has various functions like adding to database etc.The name of this file is create.js. Now I want to convert create.js code using mongodb to waterline.
me_aj, there is no possible short and simple answer for what you want. Nor there is any tool that will do what you need. Here are some pointers for what you'll need to do.
1. Learn about using waterline
It may sound obvious but you should start by getting acquainted with waterline and the best places are the waterline project and the Waterline Docs.
By then you'll know waterline is an adapter based ORM and that you'll have to decide which adapter to use. If you are using mongodb, you are probably looking at sails-mongo.
2. Config waterline to connect to the desired data store
Waterline, contrary to some other ORMs, has to be initialised before you can start using it. If you are using it outside sails you should look at these examples on how to set up a simple app. You'll also need to configure it by following the specific adapter settings. If you are connecting to mongodb check the instructions on the sails-mongo project.
3. CRUD operations
By now you've setup waterline to initialise and connect to your chosen data store, now it's time to do things with it. Similar to many other ORMs. waterline uses methods such as find, update, create and destroy to perform CRUD operations, to learn more about using these check the query methods documentation page.
This should be enough to point you in the right direction.
Hey everyone I am working on nodeJS app. I searched some modules to manage my database (orientdb).
My question is: Why we use any ORM or ODM (or why is it recommenced), because there is a module which can provide many functions to manage DB.
I am still confused what should I use orientorm (https://github.com/mav-im/orientorm) or oriento (https://github.com/codemix/oriento)
Thank in advance..
Depending on the goal and depending on the ORM, ORMs have the advantage of adding support for:
schemas / models / collections: this makes it easier to create all classes/properties and, in some cases, migrations;
validations: make it easier to verify what gets saved in the DB.
All OrientDB ORM's I've seen for node.js expose Oriento, so that makes it easy to access the underlying oriento methods for doing more complex stuff.
Having said all this I recommend you try the waterline ORM with waterline-orientdb adapter. Waterline is an adapter based ORM with support for multiple databases (including support for associations between databases). Waterline-orientdb is the adapter for OrientDB which is based on Oriento. If at any point you need to use Oriento you can call .getDB() to access Oriento's instance.
Oriento is much more mature and supported. I suggest you to go with it.
I am developing a sails.js app. In my previous sails app, I used MySQL with the default waterline ORM. I am planning to use Mongodb in my new app. I have come across limitations with waterline, one of them being, querying an association. My current app is logically intense and deals with a lot of statistical data. Is it safe to continue with waterline or replace it with Mongoose?
Waterline is getting better and supports for many features that were missing lately. Since both have association support now, any of them would be fine for this task. Only advantage is Waterline is that if the project is based on Sails, using it would be much easier.
Waterline associations
There is an example on Github for a Mongoose ORM Hook that should facilitate the disabling of Waterline and other associated hooks and enabling Mongoose as the ORM to use in your apps.