Working with mongoose and Mongo DB schema - node.js

We are working with mongoose in order to interact with Mongo DB.
We define the schema at the code level.
Now we want to move and use Mongo DB schema (At the collection level inside DB) and we dont want to maintain two types of schemas.
We wish to keep on working with mongoose because it comes with few nice features that we like.
Any idea?

Related

Is schema/model a must for reading data from mongodb database

Do we need a model/schema to just read data from a mongodb database.
I have this nodejs application with data already stored in mongodb. But the structure of the documents can change and schema diesnt allow for change.
So is it possible to read data without using a schema in mongoose or should I use the nodejs mongodb driver for this.
Thanks
Mongoose allows you to access the connection/db object, which will allow you to access your collections. You can then execute queries directly on a specific collection, without having to deal with models/schemas:
let yourCollection = mongoose.connection.db.collection('<tbd>');
const result = await yourCollection.find({...});

How to create MongoDB indexes in NodeJS app using Mongoose?

According to Mongoose documentation , the index should not be defined on the schema and should be disabled in production. I am confused as to what is being recommended. If index should not be defined in the mongoose schema then how it should be created in NodeJS app?

how to get all collection names from a specific db in mongo using nodejs

Recently I want to get all collection names from a db in mongo. And my develop environment is nodejs + express + mongoose. I tried mongooser.conncetion.db.getCollectionNames(or listCollections or collections), but any does not work. I guess the it is the version problem, maybe the latest mongoose api does not contain those functions.
Consequently, how to obtain all collection names from a db using mongoose?
Thank you so much for any help!

Does Mongoose allow for Multiple Database Connections?

I read that in a nodejs application, if we use a mongoose object using
var obj = require('mongoose');
then, wherever we create a new mongoose object (say, in another file), then the same cached copy is used throughout. In that case, how can we connect to multiple database instances of mongoDB in the same nodejs app ?
You can use mongoose.createConnection to connect to a separate database, and then using the connection object that generates, you can define models from that database using connection.model.

accessing mongodb from nodejs - common CRUD methods

I am new to mongodb and nodejs.So far I have been able to create a new mongodb database and access it via nodejs. However I want to write some generic set of methods for accessing collections (CRUD), as my list of collections will grow in number. For example I have a collection which contains books and authors
var books = db.collection('books');
var authors = db.collection('authors');
exports.getBooks = function(callback) {
books.find(function(e, list) {
list.toArray(function(res, array) {
if (array) callback(null, array);
else callback(e, "Error !");
});
});
};
Similar to this I have the method for getting authors as well.Now this is getting too repetitive as I want to add methods for CRUD operations as well. Is there a way to have common/generic CRUD methods for all my collections ?
You should take a look at Mongoose, it makes it easy to handle Mongodb from node.js, Mongoose js has a schema based solution, where each schema maps to a Mongodb collection, and you have a set of methods to manipulate these collections via models, that are obtained by compiling the schemas. I was exactly in the same place couple of months ago and have found that Mongoosejs is a good enough for all your needs.
#Dilpa - not sure if you have looked at or are utilizing Mongoose link, but it can be helpful with implementing CRUD.
I wrote my own service to handle very simple CRUD operations on mongodb documents. Mongoose is excellent, but imposes structure on the documents (which IMO goes against the purpose of mongodb--if you're going to have a schema, why not just use a relational db?).
https://github.com/stupid-genius/MongoCRUD
This service also has the advantage of being implemented as a REST API, so it can be consumed by a node.js app or others. If you send a GET request to the root path of the server, you'll get a screen that shows the syntax for all the CRUD operations (the GUI isn't implemented yet). My basic approach was to specify the db and collection on the URL path host/db/collection and then pass the doc in the POST body. The route handlers then just pass on the doc to an appropriate mongodb function; my service just exposes those methods in a pretty raw state (it does require authentication though).

Resources