How to create new Schema same as an existing one in mongoDB - node.js

I have a question in mongoDB. I have a user schema with different fields and methods. But I want to when deleting a user from this schema to place it in another schema for example deleted-users schema.
Should I create the deleted-users schema with the same exact fields but different name ?
Or is there a better way to this

Related

Confusing database with schema

I have defined db name in squelize constructor so that tables being generated by corresponding model files. Tables are successfully created in given db. I am confused with the schema which by default is public in models.So why to define schema in models if tables are properly generated in db in default postgresql server named PostgreSQL 15. if I change name of schema from public to some how ALGH it says schema doesnot exist. if it is referencing schema then what is the purpose of giving db name in sequelize constructor.
You can have multiple schemas in the same DB for different purposes like to divide sets of tables for different apps/services or by functionality.
You can indicate a schema in the Sequelize instance and it will be the default schema for all models that does not have an explicit schema OR you can indicate a schema for certain models only and other models will have the default public schema.
Of course, you need to create tables only after you decided and indicated all schemas (either in Sequelize instance and/or in certain models) you need otherwise you will get such errors you already mentioned in the post.

How to load mongoose Schema from the collection name

Ideally, for populate to work during queries in mongoose, both the schemas of that which is been populated and the calling schema needs to be imported in same file under same repo.
Now, I have a scenario where a schema called AuthorSchema was created by another internal micro-service in another repo which is pointing to the same database as my own service and I wish to populate it during my queries.
Is there a way to automatically decode the AuthorSchema from the author collection name after I've connected to the database?
E.g
let posts = await Post.find({})
.populate("author", 'first_name last_name _id').limit(10)
Where I have created postSchema in my own service but authorSchema was created by another micro-service in another repo called author-management-microservice and as such I have no access to that schema.
What I would want is something like:
let AuthorSchema = mongoose.connection.decodeSchemaFromCollectionName('author');
Such that I can modify my query to something like
let posts = await Post.find({})
.populate({path:"author", model:AuthorSchema, select:'first_name last_name _id'}).limit(10)
The reason I want something like this is because I have been battling with MissingSchemaError: Schema hasn't been registered for model "Author". whenever I try to populate author and I don't want to copy around duplicate schema files across different micro-services.
To get the schema from a registered Mongoose model, you need to access the schema specifically:
const AuthorSchema = require('mongoose').model('Author').schema;
Is there a way to automatically decode the AuthorSchema from the author collection name after I've connected to the database?
No - the schema is saved at the code level, not the database level. There is no way to know the schema just from connecting to the database.

How do I specify multiple database schemas in Diesel?

How to specify multiple schemas in Diesel ORM (Rust). I desire to have several models specified into different schema according to their domain categories. I used a similar solution with hibernate and JPA in Java( you specify the default schema in the global configuration and the rest of the schema on each entity modal).
This is what I can currently configure in diesel.toml, but it is limited to a single schema only.
[print_schema]
file = "src/schema.rs"
schema = "shema-name"
Currently, the schema field in [print_schema] is for specifying only the default schema(If no value is provided, the public schema will be searched). For tables in a schema other than the default, the table name should be given as "schema_name.table_name". More is stated here

Why use mongoose schema in Node.js

I was learning Node.js and Mongoose. Then, I cam across the term schema which is defined as the description of the structure of the data, default values and validation. So, as we know schema looks basically like this:
var TaskSchema = new Schema({
name: String,
priority: Number
});
The question I would like to ask is Why should we care about the description of structure of data that the schema allows to achieve, I mean, what is the main point in the use of schemas in mongoose? if you say, validation, then can't we achieve that with express-validator package? or...
Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
See the documentation for more information.
Schemas not only define the structure of your document and casting of properties, they also define document instance methods, static Model methods, compound indexes, and document lifecycle hooks called middleware.
This is what allows you to communicate with the database easily using mongoose. It's not just for validation.

How to retrieve schema from a mongoose collection?

using this line :
var collection = mongoose.connection.db.collection("users");
I get users s collection, now i need to get the schema from that collection ?
Is there a way to do this? thank you.
PS: I dont have any models
if you do not have any models, I'm afraid this can't be done.
the schema is the structural representation of the document, not the collection. You can use the schema (via the model) to enforce that any document added to the collection match some structure, but there's nothing that prevents you from inserting into this collection a different document without this model, that will not match the criteria
so the schema does not live in the DB, it is a part of the mongoose module, and lives in the code. and the way to access it is through the model only

Resources