One to One Relationship MongoDB (Linking)

Learn MongoDB by short way Tools Docker MongoDB Image from Docker Hub mongosh One to One Relationship (Linking) Schema author - _id (auto generate) - name - age address - _id - user_id (fk) - street - city Mongo SH Connection: $ mongosh mongodb://docker:[email protected]:55000 Pipeline: show dbs; use test; db.author.drop(); db.address.drop(); db.createCollection('author'); db.createCollection('address'); /* Preparing

Learn MongoDB by short way

Tools

  • Docker
  • MongoDB Image from Docker Hub
  • mongosh

One to One Relationship (Linking)

Schema

author
 - _id (auto generate)
 - name
 - age
 
 address
 - _id
 - user_id (fk)
 - street
 - city

Mongo SH

Connection:
$ mongosh mongodb://docker:[email protected]:55000
Pipeline:
show dbs;

use test;
db.author.drop();
db.address.drop();
db.createCollection('author');
db.createCollection('address');
/* Preparing data  for author */
db.author.insertMany([{ name:String('Tran Van A'), age:NumberInt(20)},{ name:String('Nguyen Van B'), age:NumberInt(30)},{ name:String('Le Thi C'), age:NumberInt(40)}]);
db.address.insertOne({
        street:'Hai Bà Trưng',
        city:'HCM'});
db.author.find({}).pretty();
db.address.find({}).pretty();
db.author.findOne({ name:'Tran Van A'});// Get all field with name 
db.author.findOne({ name:'Tran Van A'},{_id:true});// Get both Key and Value
db.author.findOne({ name:'Tran Van A'},{_id:true})._id;// Get only Value
// Add addresss for Auth name Tran Van A
db.address.insertOne({
        user_id: db.author.findOne({ name:'Tran Van A'},{_id:true})._id,
        street:'Nguyen Hue',
        city:'HCM'});

db.address.find({}).pretty();

// Update Address without Author

db.address.insertOne({
        street:'Hai Bà Trưng',
        city:'HCM'});
db.address.find({}).pretty();// Update Address to Author with Name
db.address.updateOne({
        street:'Hai Bà Trưng'},{
    $set:{
        user_id: db.author.findOne({ name:'Nguyen Van B'},{_id:true})._id
    }});

db.address.find({}).pretty();// From Address get User
db.address.aggregate([{
        $lookup:{
                from:"author",// Author table
                localField:"user_id",// Local Field from Address
                foreignField:"_id",// Author table, Col _idas:"users_list"// Descrise User Detail}}]).pretty();// From author get the address
db.author.aggregate([{
    $lookup:{
            from:"address",// Address table
            localField:"_id",// Local Field from Address
            foreignField:"user_id",// Author table, Col _idas:"address_info"// Descrise User Detail}}]);

db.author.find({});
db.address.find({});

Tham khảo
https://github.com/Ducmy/learn-mongodb-by-line/blob/main/README.md

Nguồn: viblo.asia

Bài viết liên quan

Cấu hình Prisma v7 Với Nest.js Mới nhất

Setup Prisma v7 trong Nest.js Bài viết dành cho ai mới học Nest.js và chọn prisma làm OR

Tấn Công Ứng Dụng Web: Mối Đe Dọa Hàng Đầu – Phần 2

viết lại nội dung này ” Phát hiện các cuộc tấn công Cross Site Scripting (XSS)

AI Chatbot 2025: Xu Hướng Tất Yếu Cho Doanh Nghiệp Dẫn Đầu

Giới thiệu AI chatbots đã trải qua một hành trình đáng kể, từ những công cụ t

Tấn Công Ứng Dụng Web: Mối Đe Dọa Hàng Đầu – Phần 1

Tấn công web là gì? Ứng dụng web là các ứng dụng cung cấp dịch vụ cho người