Populate Mongoose Document from multiple collections - node.js

I am looking for a way to populate documents beyond a document or the ref parameter.
const LibarySchema = new Schema({
books:[{
book: { type: Schema.Types.ObjectId},
bookType: { type: String }
}, { _id: false}]
});
const BookType1Schema = new Schema({
bookType: {
type: String,
default: 'lecture'
},
});
const BookType2Schema = new Schema({
bookType: {
type: String,
default: 'assingment'
},
});
This is an example of a similar project model, I am working on with 3 models. The first model hold all the information of the library books while the other models are the type of books. So during insertion of new book to the library array the bookType is added to know the collection to look for.
So my question is, is there anyway during population to check for the bookType and choose the right collection to find the book.
Also i am open for suggestion on the model.

you can just simply have a single schema for the thing you asks. Don't make unnecessary schema and make you collections bulky.. It will not be good when querying with tons of data when the system grows ...
Don't use separate schemas for storing just the types just make a single library book schema and use that to find the book you wan to find

Related

Schema design suitable for big system

I am designing a follow feature for my online books reading which had about 100k books. So I wonder which is a better way to use MongoDB.
Push all books followed into array and store as 1 document. When reach max document we split into another document
When the user followed a book we create a need documents
Please help me point out the Pros and Cons of each way or a new way better than both above way to reach this situation
const mongoose = require('mongoose');
const {comicConnection} = require('../db');
const Schema = mongoose.Schema;
const UserFollowsSchema = new Schema(
{
userId: { type: Schema.Types.ObjectId, ref: 'Users' },
follow: [{ type: Schema.Types.ObjectId, ref: 'Books' }],
},
{
timestamps: true,
}
);
module.exports = comicConnection.model(
'UserFollows',
UserFollowsSchema,
'UserFollows'
);
It is better to use the second method, you have better access and you can do calculations more easily.
Whenever a user follows a book, create a document that the user follows the book, and so on.
Then you can use this to find out how many books you have followed:
User.countDocuments({user:{USER-ID}});

Mongoose schema - nested objects with shared properties

I'm new to both MongoDB and Mongoose and trying to figure out the best way to handle creating my schema. I am more used to relational databases, but am hoping this will work with Mongo.
I am creating objects that "extend" other objects. For example, I have a person object that I want to use as the starting point for a parent or grandparent object. Both the parent and grandparent may have additional values beyond those that a base person has (I'm just including one example in each)...
const personSchema = new mongoose.Schema({
name: String,
birthDate: Date,
deathDate: Date,
});
const parentSchema = new mongoose.Schema({
//all the stuff a person has + below:
children: [personSchemas?] //[array of persons, important... parents can also be children,
multiple parents will share the same child]
parentingStyle: String,
})
const grandParentSchema = new mongoose.Schema({
// stuff that a parent has plus
grandparentingStyle: String,
})
I think the following post includes some answers that could be helpful to you:
Referencing another schema in Mongoose
Other than that, I might also suggest you'd define parents and grandparents as roles perhaps in this way:
var mongoose = require('mongoose');
const PersonSchema = new mongoose.Schema({
person_id: {
type: mongoose.Schema.Types.ObjectId
},
name: String,
birthDate: Date,
deathDate: Date,
//the following is if you want later to fetch persons by role
role: {
type: string,
enum: ["parent", "grandParent","child"],
}
});
//Then you could do it this way
const ParentSchema = new mongoose.Schema({
//all the stuff a person has + below:
children: [personSchemas],
parentingStyle: String
})
//Or this way
const ParentSchema = new mongoose.Schema({
//all the stuff a person has + below:
children: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Person'
}
parentingStyle: String,
})
const GrandParentSchema = new mongoose.Schema({
// same as for parents (ether way)
// stuff that a parent has plus
grandparentingStyle: String
})
In non-relational databases, it really depends on what you'd like to do with the data later on. The "joints" (in comaprison to relational DB) are much simpler to create, either by referencing an id or fetching a whole array (like you did) or simply by making more elaborate queries later on.

How to reference a Mongoose Schema that hasn't been registered yet

This question is similar to: this question
Working with Mongoose, I have something like the following code. (I've prefixed the files with numbers, so I can load them in the order they appear in the 'models' directory, but still control the loading order.)
In 100-employee.server.model.js:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var EmployeeSchema = new Schema({
name: {
type: String,
default: ''
},
company: {
type: Schema.ObjectId,
ref: 'Company'
},
subordinates: [EmployeeSchema],
});
mongoose.model('Employee', EmplyeeSchema);
Then, in 200-company-server.js, I have:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var CompanySchema = new Schema({
name: {
type: String,
default: ''
},
CEO: {
type: Schema.ObjectId,
ref: 'Employee'
}
});
mongoose.model('Company', CompanySchema);
Obviously, this doesn't work, since Company is referenced before it is registered. And, loading these in the opposite order doesn't work for the same reason. What I need, is a logical data structure like:
{
name: 'Acme, Inc',
CEO: {
name: 'Karen',
subordinates: [{
name: 'Bob',
subordinates: [{
name: 'Lisa',
subordinates: []
},
name: 'Jerry',
subordinates: []
}]
}]
}
}
(I think I got all of my brackets in place. I just typed that JSON to illustrate the need.)
I could just use an ObjectId for 'company' in EmployeeSchema, but it doesn't fix the problem. I still get a complaint that Company hasn't been registered.
Someone will ask for the use case, so here it is:
I have a bunch of companies.I have a hierarchy of employees of a company. And, I've got a bunch of companies. For each company, I need to know the CEO, without having to search all of my employees for the one with no parent, that has an ObjectId ref Company, but that still runs into the same problem.
Any suggestions?
OK, I've come up with an answer, or at least a workaround. Doesn't feel very elegant. In fact I actively dislike it, but it works. Would love to see a better solution. In 100-company-server.js, define the parent entity first, like so:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var CompanySchema = new Schema({
name: {
type: String,
default: ''
},
CEO: {
name: { // This is the company name, so probably a bad example
type: String,
default: ''
}, {
shoeSize: Number
}, etc ...
}
});
mongoose.model('Company', CompanySchema);
The key thing to notice here is that instead of making CEO an ObjectId ref 'Employee' I supplied an object that uses the same properties as my Employee schema. Depending on how you want to use it, you may have to coerce that into an Employee object in the controller. Or, there might be a clever way to use a virtual to do the same thing (http://mongoosejs.com/docs/guide.html) But, the virtual would just have to be defined AFTER the EmployeeSchema and model.
In 200-employee.server.js, something like the following:
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
CompanySchema = mongoose.model('Company').schema; // so you can use this
// after your Employee
// is defined
var EmployeeSchema = new Schema({
name: {
type: String,
default: ''
},
company: {
type: Schema.ObjectId,
ref: 'Company'
},
subordinates: [], // Just an empty array. You fill it with Employees in the controller. You could use a virtual to do the same thing, probably.
});
mongoose.model('Employee', EmployeeSchema);
So, it's kind of ugly, but it works.
Key points:
In defining CompanySchema, you don't use an ObjectId ref 'Employee'.
Instead, you define an object that looks like a Company object.
Since this object doesn't have an _id, you have to work around that in the
controller. In my case, this works fine. But, for many/most use cases, this
probably would not. You'd have to work around this with clever virtuals
and/or the controller.
Use a simple array, without type, to store your children (suborinates in
this example.
The array works just fine, and returns objects just as if you used an array
of ObjectId ref Employee.
Making the Company refer to an Employee (which hasn't been defined yet), you
have to convert a generic object into an Employee object. But, there are
many ways to do that.
There is, undoubtedly, a better way to do this.

How to declare schematype/datatype of a field in mongoose which is the objetid of an item in another collection?

I am a bit confused by something in MongoDB, when using Mongoose, since I am new to it. I have two collections: authors and posts.
each author in the authors collection have the mongodb inbuilt id associated with them, which as per my understanding is of the schematype/datatype Objectid.
Now in my posts collection, I have a field which has is called author, which should have the value of the author's Objectid, similar to the foreign key concept in SQL.
My question is, which schematype/datatype should I declare the author field in the posts collection? Should I put it as Objectid? If so, would it not auto-increment and not be settable?
Mockup Of Schemas:
var authors = new mongoose.Schema({
name: String,
email: String
});
var posts = new mongoose.Schema({
title: String,
author: **??**
});
Any help would be hugely appreciated!
You can use population for that:
var authors = new mongoose.Schema({
name: String,
email: String
});
var posts = new mongoose.Schema({
title: String,
author: { type: Schema.Types.ObjectId, ref: 'Author' }
// assuming the model will be called 'Author'
});
To use:
// Get 'author' some how (create a new one, query the
// database for existing authors, etc);
...
// Create a new post and associate it with 'author' by referencing its `_id`:
var post = new Post({
...
author : author._id
});
post.save(...);
The documentation linked above also shows how you can get Mongoose to automatically retrieve the author document when you're querying posts.

Use mongoose populate to "join" matching subrecords

Using node.js, mongoose (3.5+), mongodb. Have got two collections in the DB:
var AuthorSchema = new mongoose.Schema({
name: { type: String },
});
var StorySchema = new mongoose.Schema({
title: { type: String },
author: { type: type: Schema.Types.ObjectId },
});
What I would like to do is retrieve an author and populate it with a subcollection (say, "stories") that is looked up from Story and match the author. Yes, much like a SQL join.
All of the examples out there work on the AuthorSchema having an array of objectids that reference StorySchema objects - that works fine. But I want to go the opposite direction; partly due to minimizing insert/updates. If I follow the example, adding a new store requires adding a new Story document and updating the Author. I want to just insert a new Story that references the Author.
I suspect that populate() is the right way to go, but can't get it to work. I'm doing something like this:
Author.find({name: 'Asimov').populate({
path: 'stories',
model: 'Story',
match: {'author': this['_id']},
}).exec(function(err, authors) {
console.log(authors);
})
But this doesn't return any stories member in the returned authors. Is this not a populate() solution? Do I really need to structure the schemas differently? Or is there some other feature of mongoose/mongo that would do what I'm looking for.
In the story schema, do this:
author: { type: type: Schema.Types.ObjectId, ref:'Author' }, //or whatever the model name is
then you can run
Story.find({}).populate('author').exec(function(err,stories) {...});

Resources