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

9 Mẹo lập trình Web “ẩn mình” giúp tiết kiệm hàng giờ đồng hồ

Hầu hết các lập trình viên (kể cả những người giỏi) đều tốn thời gian x

Can GPT-4o Generate Images? All You Need to Know about GPT-4o-image

OpenAI‘s GPT-4o, introduced on March 25, 2025, has revolutionized the way we create visual con

Khi nào nên dùng main, section, article, header, footer, và aside trong HTML5

HTML5 đã giới thiệu các thẻ ngữ nghĩa giúp cấu trúc nội dung web một cách có

So sánh Webhook và API: Khi nào nên sử dụng?

Trong lĩnh vực công nghệ thông tin và phát triển phần mềm, Webhook và API là hai th