xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

MongoDB playground All In One

MongoDB playground All In One

MongoDB REPL

image

https://mongoplayground.net/

db={
  "teacher": [
    {
      "_id": ObjectId("64fee9b54273ac2234441225"),
      "teacherid": ObjectId("64f1d72a4331bc8fc4c5930f"),
      "name": "xgqfrms"
    }
  ],
  "users": [
    {
      _id: ObjectId("64f1d72a4331bc8fc4c5930f"),
      name: "eric"
    }
  ]
}

aggregate

db.teacher.aggregate([
  {
    $match: {
      "teacherid": ObjectId("64f1d72a4331bc8fc4c5930f")
    }
  },
  {
    $lookup: {
      from: "users",
      localField: "teacherid",
      foreignField: "_id",
      as: "student"
    }
  },
  {
    $unset: "name"
  }
])
// ]).explain()
// ]).pretty()
// Unsupported method: only find(), aggregate(), update() and explain() are supported

demos

mongo

$ mongo

# replace the `YOUR_DATABASE_NAME` with your real database name.
> use YOUR_DATABASE_NAME

# update Teacher collection ✅
> db.Teacher.update(
  {"teacherid" : "64f1d72a4331bc8fc4c5930f"},
  {
    $set: {"teacherid" : ObjectId("64f1d72a4331bc8fc4c5930f")}
  });

> db.Teacher.aggregate([
    {
        $match: {
            "teacherid": ObjectId("64f1d72a4331bc8fc4c5930f")
        }
    },
    {
        $lookup: {
            from: "user",
            localField: "teacherid",
            foreignField: "_id",
            as: "student"
        }
    },
    {
        $unset: "name"
    }
]).pretty();

image

Node.js



MongoDB 跨表查询

https://www.cnblogs.com/xgqfrms/p/17724984.html#5215297

refs

https://stackoverflow.com/questions/77193151/how-to-get-data-from-another-collection-in-mongodb/77194045#77194045

https://www.cnblogs.com/xgqfrms/p/17724984.html#5215324



©xgqfrms 2012-2025

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(34)  评论(1编辑  收藏  举报
相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2022-09-29 如何组织团队技术分享的一点心得和建议 All In One
2022-09-29 js bitwise operators All In One
2022-09-29 我为政府网站找错 All In One
2022-09-29 HTTPS & HSTS All In One
2021-09-29 揭秘游戏充值内幕-盗刷黑卡 All In One
2020-09-29 React 组件之间通信 All in One
2020-09-29 LeetCode 两数之和 算法题解 js 版 All In One
点击右上角即可分享
微信分享提示