1

This is the first line used to describe moongoosejs.com

mongodb object modeling for node.js

I am currently using Mongoose but because I have never used bare mongoDB perhaps I don't understand the line.

I know what all the words means separately but not together. Does object refer to the object literals I use to define my schemas?

Or something more specific?

  • When you 'model' a database object, you have some local in-memory representation of something that belongs in the database. Also known as "entities". Your schema lets you create objects that model your data objects. – Wyck Oct 27 '21 at 19:23
  • Maybe I should have asked what one word means ...`object` ... this is not the JavaScript object literal I thought it meant but a database object? What is a database object ? –  Oct 27 '21 at 19:25
  • I have written schemas in Mongoose, and am able to perform all the CRUD operations for my app, I just want to be able to understand how the code matches up to the concepts / vocabulary ... –  Oct 27 '21 at 19:27
  • `const Tank = mongoose.model('Tank', schema);` Creates a model from a schema. `const small = new Tank({ size: 'small' });` creates an object from the model. – Wyck Oct 27 '21 at 19:27
  • mongo store in an `object`, which doesn't limits in field types instead mongoose stored in an `object` with predefined field types. – hUwUtao Oct 27 '21 at 19:27
  • ...ahah ... these terms get overused - model, object ... but that is called instantiation ... you instantiate the model to get the object ( different from the object literal I used to create the schema ) ... –  Oct 27 '21 at 19:30
  • 1
    which brings us back to "object modeling" ... in this case model refers to the model you referred to I take it? Basically from a schema, I can create a model, and from a model, I can instantiate to get an object ... –  Oct 27 '21 at 19:32
  • "modeling objects" might make more sense, either way, I think they need a better description, as these are some very general words used in multiple ways. –  Oct 27 '21 at 19:33

2 Answers2

0

mongoDB objects: documents stored in a mongoDB database

modeling: A database can have collections and each collection is a logical set of documents. Models are the basis of collections. Collection is a bag of documents and this bag goes well with a Model because it makes it easier to decide which document should enter into bag and which pattern all documents should share(Schema Validation).

for node.js: Obviously, it means we can't use mongoose on PHP, Python etc.

So, what if you don't model mongoDB objects with mongoose? Well I haven't used pure MongoDB but I can say that without mongoose:

  1. Your database will be loosely structured
  2. It will contain more lines of code for basic database operations.

For more info: Why do we need, what advantages to use mongoose

Abdulhakim
  • 620
  • 8
  • 11
0

It's an object representation of what's stored in your database. Anyway I have used mongoose a lot, and I tend to stay away from it because nodejs is not an object oriented language and handling object feels messy.

If you really want to use mongoose it would be better to use typescript (with typegoose) to make the use of objects easier.

Finally, I don't use mongoose or typegoose and just write raw query because mongodb is not a relational DB (and that's what an ORM is made for), it's true that you can work with relationship but it's not the way it's meant to be used.