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



(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

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-2021

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

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


posted @ 2023-09-29 11:40  xgqfrms  阅读(32)  评论(1编辑  收藏  举报