collections (table names in other dbs) in mongodb - javascript

I created an express server for connection to my mongodb. Before I start, everything works fine.
But I dont understand some things.
I made a collection in my mongodb, projects (with an 's' at the end)
In my code I made a project.model.js like: const Project = mongoose.model("Project", ...) module.exports = Project
Elsewhere I have db.project = require("./project.model");
Everything works like a harm, creating, updating, deleting. But I dont understand. My collections in my mongodb don't have the same name. The names are Users, Materials and in my code I use user, material. Maybe it is a stupid question but how does this work?

It is the default behavior of mongoose to convert singular to plural model names.
To fix your issue & to convert your model name from plural to singular or any other name, You can write your code as shown below
const Project = mongoose.model("Project", ProjectSchema, 'Project')
module.exports = Project
Now you could see the collection name as Project.
The third parameter in the mongoose.model is the collection name.

Related

Why mongoose.model() is required to get data from MongoDB?

I want to read data from the mongodb with mongoose, but every time it requires creating a model. Why?
I thought model are just like templates to insert data to MongoDB.
Can anyone describe what exactly mongoose.model() is and how it works?
I tried
const Model = mongoose.model(mongoose.Schima())
Without object in it
And it worked as well!!!
How does mongoose.model get data in background?
Thank you...
Mongoose models are much more than just templates on how to store data in the database: they perform type conversion, provide validation, have pre/post hooks, provide easy methods for population, and much more.
You don't need to use a full model to retrieve data from the database (in fact, you don't even need Mongoose at all), but you'll lose all the additional features.

can I reach mongodb collections without creating model? (mongoose)

I'm trying to finish a project to get a part-time job and I got stuck in here.
The problem is: There are 2 JSON files given for products and categories. I uploaded these JSON files using MongoDB Compass. How can I reach these 2 documents and their collections in my code? Because I need to use them to create a website. Is there a way to do this, or:
Do I have to create Schema and Model, and after that read objects from JSON files in my code, create instances and save them one by one to my database?
You can actually use the db property of your mongoose instance and issue queries directly as needed - if you need to bypass your Models for some reason.
Models and defined schema are what many find to be the benefit of using Mongoose. You don't have to use Mongoose and can use the official Nodejs driver too.
To access db directly in Mongoose:
const mongoose = require('mongoose');
mongoose.connect(`mongodb://localhost/mydb`, {useNewUrlParser: true, useUnifiedTopology: true, autoIndex: false});
const db = mongoose.connection;
db.db.collection('mycollection').find({});

Auto-generate model definitions from existing ArangoDB database

I'm using ArangoDB 3.4 and planning to use an MVC framework like Backbone.js (or whatever is recommended). Is there a way to auto-generate the models from the existing database to reduce the amount of boilerplate code I have to write by hand?
For example, I am looking at the aye-aye TodoMVC demo. It has this model:
const joi = require('joi');
exports.Model = {
_key: joi.string().optional(),
_id: joi.string().optional(),
_rev: joi.string().optional(),
completed: joi.boolean().optional(),
order: joi.number().optional(),
title: joi.string().optional()
};
Writing a few by hand is no problem. My database will ultimately require many of these models. Are there any tools that I can use with ArangoDB that will help automate this by producing scaffold code?
What I have in mind is possibly something like Python's inspectdb command:
http://docs.djangoproject.com/en/dev/ref/django-admin/#inspectdb
inspectdb
Introspects the database tables in the database pointed-to by the DATABASE_NAME setting and outputs a Django model module (a models.py file) to standard output.
Use this if you have a legacy database with which you'd like to use Django. The script will inspect the database and create a model for each table within it.
As you might expect, the created models will have an attribute for every field in the table.
If there are entirely different approaches to doing this with ArangoDB and javascript please point me in the right direction.
django-admin inspectdb [table [table ...]] targets relational databases where tables have schema and because of that it's possible to generate model
ArangoDB is NoSQL with schemaless collections with ability to store various JSON documents types and because of that you'll need to get schema per document type.
While using fullstack javascript approach you can put your model in js module and use it on both front and backend.
for us, most reliable and scalable approach is based on Typescript as master with following-ish workflow
turn JSON to TS via VS Code extension json2ts.com
then you can
generate JSON Schema via typescript-json-schema
generate UML diagram with tsviz
convert JSON Schema to joi with enjoi
generate forms from JSON schema (front-end framework specific)

Db versioning/new changes with loopback

My service is built using Node + Loopback + Mongo.
I would like to drop the db at the startup if and only if at least one of the following two asserts is true:
any model schema has changed (note that I cannot use isActual() because that's usable only with relational DBs)
new data has been added/removed from the default data-set definition files (data folder)
I'm currently versioning the db using a custom logic comparing version table and a version unique var but I wonder if there is an easy way to check the two asserts above.
Any idea? Ty

Import multiple collections (nodejs, mongodb)

I'm new with node.js and mongodb.
In my database, I have several collections (one for users, one for article, and in the future one more...).
In my server.js file, I would like to be able to write in each of these collections.
Here is the code I use, but I'm not able to access all my collections... Have you an ideas to make that possible?
var databaseUrl = "mydb"; // "username:password#example.com/mydb"
var collections = ["users", "article", "reports", "archery"]
var db = require("mongojs").connect(databaseUrl, collections);
Thank you
You don't have to know all of your collections in advance to use mongojs, you can access a collection dynamically using db.collection('name_of_collection') and use it just like an existing collection. This call will also cache it so that next time, you can say db.name_of_collection.
There's a bunch of examples at their git hub page:
https://github.com/gett/mongojs
Good luck.

Categories