RELATIONSHIPS INCLUDE
ONE - TO -ONE (1x 1) ONE ENTITY OF A TABLE IS IN RELATION WITH ONLY ONE ENTITY OF ANOTHER.
ONE - TO -MANY (1x N) ONE ENTITY IS IN RELATION WITH MANY
MANY - TO - MANY(NxN) MANY ENTITIES ARE IN RELATION WITH MANY OTHER.
ONE- TO-MANY IS MORE HARDER TO BE REPRESENTED THAN ONE - TO - ONE AND MANY - TO - MANY
ONE- TO -MANY
IT IS CATEGORIZED INTO 3 BASED ON MANY
MANY CAN BE IN 3 TYPES
1.ONE -TO-FEW: SMALL NUMBER (2,5 ,100,500)
STORE THE CHILD DOCUMENT INSIDE PARENT
TAKE ZOMATO OR AMAZON FOR EXAMPLE, A SINGLE USER CAN HAVE MULTIPLE ADDRESSES.
HERE WE DONOT CREATE TWO MODELS FOR USER AND ADDRESS, AS ADDRESS IS ASSOCIATED WITH USER DIRECTILY, WE STORE THE CHILD DOCUMENT INSIDE PARENT.
const mongoose = require("mongoose");
const { Schema } = mongoose;
main()
.then(() => {
console.log("connection successful");
})
.catch((err) => {
console.log(err);
});
async function main() {
await mongoose.connect("mongodb://127.0.0.1:27017/relations");
}
const userSchema = new Schema({
userName: String,
addresses: [{
location: String,
city: String,
}],
});
const User = mongoose.model("User", userSchema);
const addUsers = async () => {
let user1 = new User({
userName: "abhi",
addresses:[ {
location: "wrngl X road",
city: "kmm",
}],
});
user1.addresses.push({ location: "mnglply", city: "hyd" });
let result = await user1.save();
console.log(result);
};
addUsers();
2.IN THOUSANDS (10^3 X n)
A REFERENCE IS STORED TO THE CHILD DOCUMENT IS STORED INSIDE PARENT.
TWO MODELS ARE CREATED. A REFERENCE OF ONE IS STORED IN THE OTHER FOR ACCESSING.
HERE,ORDER AND CUSTOMER ARE THE TWO MODELS.
EACH ORDER HAS AN UNIQUE ID THAT IS USED AS REFERENCE. TO USE IT AS A REFERENCE WE USE, orders: [
{
type: Schema.Types.ObjectId,
ref: "Order",
},
],
ORDERS ARE PUSHED.
const mongoose = require("mongoose");
const { Schema } = mongoose;
main()
.then(() => {
console.log("connection successful");
})
.catch((err) => {
console.log(err);
});
async function main() {
await mongoose.connect("mongodb://127.0.0.1:27017/relations");
}
const orderSchema = new Schema({
item: String,
price: Number,
});
const customerSchema = new Schema({
name: String,
orders: [
{
type: Schema.Types.ObjectId,
ref: "Order",
},
],
});
const Order = mongoose.model("Order", orderSchema);
const Customer = mongoose.model("Customer", customerSchema);
const addOrders = async () => {
let res = await Order.insertMany([
{ item: "bat", price: 3000 },
{ item: "grace ball", price: 750 },
{ item: "gloves", price: 500 },
{ item: "helmet", price: 871 },
]);
console.log(res);
};
addOrders();
const addCustomer = async () => {
let cust1 = new Customer({
name: "abhi",
});
let order1 = await Order.findOne({ item: "bat" });
let order2 = await Order.findOne({ item: "gloves" });
cust1.orders.push(order1);
cust1.orders.push(order2);
let res = await cust1.save();
console.log(res);
};
addCustomer();


HERE ONLY THE ID OF THE ORDER IS STORED, IF YOU WANT TO STORE THE ENTIRE OBJECT WE CAN USE POPULATE METHOD.
const findCustomer = async () => {
let res = await Customer.find({}).populate("orders");
console.log(res);
console.log(res[0]);
};
findCustomer();
3. A DATA POINT THAT IS CONNECTED TO CRORES.
ONE TO SQUILLIONS
A REFERENCE TO THE PARENT IS STORED IN THE CHILD.OPPOSITE TO 2ND APPROACH.
TAKE INSTA OR TWITTER FOR EXAMPLE,A SINGLE USER CAN CREATE MANY NO.OF POSTS IN HIS LIFETIME.
const mongoose = require("mongoose");
const { Schema } = mongoose;
main()
.then(() => {
console.log("connection successful");
})
.catch((err) => {
console.log(err);
});
async function main() {
await mongoose.connect("mongodb://127.0.0.1:27017/relations");
}
const userSchema = new Schema({
name: String,
email: String,
});
const postSchema = new Schema({
content: String,
likes: Number,
user: {
type: Schema.Types.ObjectId,
ref: "User",
},
});
const User = mongoose.model("User", userSchema);
const Post = mongoose.model("Post", postSchema);
const addData = async () => {
let user1 = new User({
name: "kohli",
email: "cupnamde@gmail.com",
});
let post1 = new Post({
content: "ee sala cup namde",
likes: 49,
});
post1.user = user1;
await user1.save();
await post1.save();
};
addData();
const findData = async () => {
// let result = await Post.find({}).populate("user");
IF U WANT TO PRINT ONLY NAME
let result = await Post.find({}).populate("user","name");
console.log(result);
};
findData();
Comments
Post a Comment