What are Mongoose ODM's ObjectId? - node.js

Looking at the Mongoose ODM docs, it doesn't really say much about what are ObjectId's and how they can be used. I think its something like foreign keys in MongoDB?
If so, Embedded Documents seem to achieve the same purpose, when do I use which?

It would be very worthwhile to read the MongoDB documentation or a quick MongoDB intro such as The Little MongoDB Book (it's free) for some background on MongoDB concepts.
To answer your question:
An ObjectID is a unique 12-byte identifier which can be generated by MongoDB as the primary key (_id) for a collection. There is a specification for the ObjectID.
A DBRef (database reference) is an ObjectID referencing an object in another collection. A DBRef does require require another query to fetch the related object, and is a convention supported by the client drivers rather than MongoDB server. The Mongoid equivalent is called referenced relations.
Embedded documents are nested arrays or subdocuments within a document. In Mongoid these are embedded relations.
The approach to data modelling and schema design in MongoDB is very different from relational databases. There are (intentionally) no joins or foreign keys, but the document-oriented approach allows large amounts of related data to be stored and fetched in a single document. Depending on how you plan to query and update your data, embedding or linking may be a more suitable choice. The schema design page on the MongoDB wiki has some helpful tips to get you started.

Related

MongoDB $lookup vs Mongoose populate

I have seen this and other similar titled questions, none answer my question.
I was going through the mongoose documentation where I read
MongoDB has the join-like $lookup aggregation operator in versions >=
3.2. Mongoose has a more powerful alternative called populate(), which lets you reference documents in other collections.
How does populate() in mongoose work that makes it more powerful than MongoDB's $lookup?
Isn't mongoose a tool that helps nodejs users work with mongodb. If so how can mongoose have functionalities that MongoDB does not? Like populate()?
Does mongoose's populate() method use MongoDB's $lookup behind the scenes?
Thanks to a github thread shared by Grégory NEUT in the question's comments I have been able to establish certain facts:
Mongoose's populate() method does not use MongoDB's $lookup behind the scenes. It simply makes another query to the database.
Mongoose does not have functionalities that MongoDB does not have. populate() just makes two or more queries.
How does populate() in mongoose work that makes it more powerful than
MongoDB's $lookup?
In my opinion, there are places to use populate() and others to use $lookup. For more complex queries $lookup in an aggregation pipeline would work best.

Azure Cosmos DB Update Pattern

I have recently started using Cosmos DB for a project and I am running into a few design issues. Coming from a SQL background, I understand that related data should be nested within documents on a NoSQL DB. This does mean that documents can become quite large though.
Since partial updates are not supported, what is the best design pattern to implement when you want to update a single property on a document?
Should I be reading the entire document server side, updating the value and writing the document back immeadiately in order to perform an update? This seems problematic if the documents are large which they inevitably would be if all your data is nested.
If I take the approach of making many smaller documents and infer relationships based on IDs I think this would solve the read/write immeadiately for updates concern but it feels like I am going against the concept of a NoSQL and in essence I am building a relational DB.
Thanks
Locking and latching. That's what needs to happen if partial updates become possible. It's a difficult engineering problem to keep a <15ms write latency SLA with locking.
This seems problematic if the documents are large which they inevitably would be if all your data is nested.
Define your fear — burnt Request Units, app host memory, ingress/egress network traffic? You believe this is a problem but you're not stating concrete results. I'm not saying you're wrong or doubting the efficiency of the partial update approach, i'm just saying the argument is thin.
Usually you want to JOIN nothing in NoSQL, so i'm totally with you on the last paragraph.
Whenever you are trying to create a document try to consider this:
Does the part of document need separate access . If yes then create a referenced document and if no then create a embedded document.
And if you want to know what to choose, i think you should need to take a look at this question its for MongoDb but will help you Embedded vs Referenced Document
Embed or Reference is the most common problem I face while designing document structure in NoSQL world.
In embedded relationship, child entities has been embedded into the parent document. In Reference relationship, child entities in separate documents and their parent in another document, basically having two (or more) types of documents.
There is no one relationship pattern fits all. The approach you should take depends on the Retrieve and Update to be done on the data is being designed.
1.Do you need to retrieve all the child entities along with the parent entities? If Yes, use embedded relationships.
2.Do your use case allow entities being retrieved individually? This case use relationship pattern.
Majority of the use cases I have worked, I used relationship pattern. For example: Social Graph (Profiles with Relationship Tree), Proximity Points (GeoJSON based proximity search), Classified Listing etc.
Relationship Pattern is also easier to update and maintain, as the entities are stored in individual documents.
Partial Updates are now supported by Cosmos DB:
Azure Cosmos DB Partial Document Update feature (also known as Patch
API) provides a convenient way to modify a document in a container.
Currently, to update a document the client needs to read it, execute
Optimistic Concurrency Control checks (if necessary), update the
document locally and then send it over the wire as a whole document
Replace API call.
Partial document update feature improves this experience
significantly. The client can only send the modified properties/fields
in a document without doing a full document replace operation
Read more here: https://learn.microsoft.com/en-us/azure/cosmos-db/partial-document-update

Mongoose: Schema vs Model?

When looking at tutorials there is often a delineation between a schema and a model, particularly when dealing with mongoose/mongodb.
This makes porting over to postgresql somewhat confusing, as 'models' don't seem to exist under that system. What is the difference the two approaches?
For example, what would be a postgres/sql ORM equivalent of this line?
(mongoose and express.js):
var userSchema = schema.define('local', {
username: String,
password: String,
});
module.exports = mongoose.model('User', userSchema);
In mongoose, a schema represents the structure of a particular document, either completely or just a portion of the document. It's a way to express expected properties and values as well as constraints and indexes. A model defines a programming interface for interacting with the database (read, insert, update, etc). So a schema answers "what will the data in this collection look like?" and a model provides functionality like "Are there any records matching this query?" or "Add a new document to the collection".
In straight RDBMS, the schema is implemented by DDL statements (create table, alter table, etc), whereas there's no direct concept of a model, just SQL statements that can do highly flexible queries (select statements) as well as basic insert, update, delete operations.
Another way to think of it is the nature of SQL allows you to define a "model" for each query by selecting only particular fields as well as joining records from related tables together.
In other ORM systems like Ruby on Rails, the schema is defined via ActiveRecord mechanisms and the model is the extra methods your Model subclass adds that define additional business logic.
A schema is fundamentally describing the data construct of a
document (in MongoDB collection). This schema defines the name of each item of data, and the type of data, whether it is a string, number, date, Boolean, and so on.
A model is a compiled version of the schema. One instance of the model will map to one document in the database.
It is the model that handles the reading, creating, updating, and deleting of documents.
A document in a Mongoose collection is a single instance of a model. So it makes sense that if we're going to work with our data then it will be through the model.
A single instance of a model (like a User instance in var User = mongoose.model('User', userSchema);) maps directly to a single document in the database.
With this 1:1 relationship, it is the model that handles all document interaction - creating, reading, saving, and deleting. This makes the model a very powerful tool.
Taken from "Mongoose for Application Development", by Simon Holmes, 2013
I imagine models as classes created from a schema (maybe I am mistaken).
MongoDB stores everything in BSON , which is a binary format. A simple Hello World BSON document might look like this internally:
\x16\x00\x00\x00\x02hello\x00\x06\x00\x00\x00world\x00\x00. A computer can deal with all that mumbo-jumbo, but that's hard to read for humans. We want something we can easily understand, which is why developers have created the concept of a database model. A model is a representation of a database record as a nice object in your programming language of choice. In this case, our models will be JavaScript objects. Models can serve as simple objects that store database values, but they often have things like data validation, extra methods, and more. As you’ll see, Mongoose has a lot
of those features.
Taken from "Express in Action", by Evan Hahn, 2016
In Short:
A Mongoose model is a wrapper on the Mongoose schema. A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.
Reference: Introduction to Mongoose for MongoDB - FCC

Whats the difference between ORM and ORP?

What is the difference between Object Relational Mapping(ORM) and Object Relational Persistence(ORP)?
From what i know ORM is a framework for mapping relational tables to application domain objects and relationships between them. So in ORM you would already have a Persistent data structure?
ORP consists of:
Entities
Database connection
Database
Mapping (ORM)
Etc..
We could almost say it is the same thing since ORM is a term which is used as ORP. Both are used to indicate the same thing.

CouchDB best practices - all entities in a single db vs a new db for each entity?

I am starting with CouchDB. What is the accepted best practice to utilize databases? A single database per application storing all kinds of entities with smth like "_type" property to discriminate one from another or a separate database for each kind of entity?
One database with lots of different types of document, so you can take advantage of View Collation. Note that top-level names prefixed with an underscore, e.g. _type, are reserved for CouchDB.

Resources