MongoDB使用引用的示例

 更新时间:2026年03月17日 10:21:47   作者:Victor356  
在MongoDB中,引用(Reference)是一种在文档之间建立关系的方式,与嵌入式文档不同,引用是通过存储其他集合中文档的标识符来建立关系,这种方式类似于SQL中的外键,适用于需要多个独立集合之间,本文介绍MongoDB什么是引用,感兴趣的朋友跟随小编一起看看吧

在MongoDB中,引用(Reference)是一种在文档之间建立关系的方式。与嵌入式文档不同,引用是通过存储其他集合中文档的标识符来建立关系。这种方式类似于SQL中的外键,适用于需要多个独立集合之间建立关系的场景。

引用的特点

  • 分离数据:引用将相关数据分离到不同的集合中,有助于减少数据冗余。
  • 独立更新:引用允许独立更新相关数据,而不需要更新整个文档。
  • 适合一对多和多对多关系:引用特别适用于复杂的数据模型,比如一对多和多对多关系。

使用引用的示例

以下示例展示了如何在MongoDB中使用引用。我们将使用Node.js和MongoDB的驱动进行操作。

安装MongoDB的Node.js驱动

npm install mongodb

插入包含引用的数据

const { MongoClient, ObjectId } = require('mongodb');
async function insertData() {
    const uri = "mongodb://localhost:27017";
    const client = new MongoClient(uri, { useUnifiedTopology: true });
    try {
        await client.connect();
        const db = client.db('myDatabase');
        const usersCollection = db.collection('users');
        const ordersCollection = db.collection('orders');
        await usersCollection.deleteMany({}); // 清空用户集合
        await ordersCollection.deleteMany({}); // 清空订单集合
        // 插入用户数据
        const users = await usersCollection.insertMany([
            { name: "Alice" },
            { name: "Bob" },
            { name: "Charlie" }
        ]);
        // 插入订单数据,并使用用户的ObjectId作为引用
        await ordersCollection.insertMany([
            { userId: users.insertedIds[0], amount: 50.5, date: new Date("2022-01-01") },
            { userId: users.insertedIds[0], amount: 100.0, date: new Date("2022-02-01") },
            { userId: users.insertedIds[1], amount: 75.0, date: new Date("2022-01-15") },
            { userId: users.insertedIds[2], amount: 200.0, date: new Date("2022-03-01") }
        ]);
        console.log("Data inserted");
    } finally {
        await client.close();
    }
}
insertData().catch(console.error);

在上面的代码中,orders 集合中的每个文档都包含一个 userId 字段,该字段引用了 users 集合中的用户文档的 _id

查询包含引用的数据

async function queryData() {
    const uri = "mongodb://localhost:27017";
    const client = new MongoClient(uri, { useUnifiedTopology: true });
    try {
        await client.connect();
        const db = client.db('myDatabase');
        const usersCollection = db.collection('users');
        const ordersCollection = db.collection('orders');
        // 查询某个用户及其所有订单
        console.log("\nQuery a user and their orders:");
        let user = await usersCollection.findOne({ name: "Alice" });
        let orders = await ordersCollection.find({ userId: user._id }).toArray();
        console.log({ user, orders });
        // 查询所有订单及其对应的用户信息
        console.log("\nQuery all orders and their corresponding users:");
        let results = await ordersCollection.aggregate([
            {
                $lookup: {
                    from: 'users',  // 要关联的集合
                    localField: 'userId',  // orders 集合中的字段
                    foreignField: '_id',  // users 集合中的字段
                    as: 'user'  // 输出数组字段
                }
            },
            {
                $unwind: '$user'  // 展开数组
            }
        ]).toArray();
        console.log(results);
    } finally {
        await client.close();
    }
}
queryData().catch(console.error);

在这个示例中,我们演示了如何查询包含引用的数据:

  1. 查询某个用户及其所有订单
  2. 查询所有订单及其对应的用户信息

运行这个脚本后,你会得到如下结果(示例输出):

// 查询某个用户及其所有订单
Query a user and their orders:
{
  user: { _id: new ObjectId("..."), name: 'Alice' },
  orders: [
    { _id: new ObjectId("..."), userId: new ObjectId("..."), amount: 50.5, date: 2022-01-01T00:00:00.000Z },
    { _id: new ObjectId("..."), userId: new ObjectId("..."), amount: 100, date: 2022-02-01T00:00:00.000Z }
  ]
}
// 查询所有订单及其对应的用户信息
Query all orders and their corresponding users:
[
  {
    _id: new ObjectId("..."),
    userId: new ObjectId("..."),
    amount: 50.5,
    date: 2022-01-01T00:00:00.000Z,
    user: { _id: new ObjectId("..."), name: 'Alice' }
  },
  {
    _id: new ObjectId("..."),
    userId: new ObjectId("..."),
    amount: 100,
    date: 2022-02-01T00:00:00.000Z,
    user: { _id: new ObjectId("..."), name: 'Alice' }
  },
  {
    _id: new ObjectId("..."),
    userId: new ObjectId("..."),
    amount: 75,
    date: 2022-01-15T00:00:00.000Z,
    user: { _id: new ObjectId("..."), name: 'Bob' }
  },
  {
    _id: new ObjectId("..."),
    userId: new ObjectId("..."),
    amount: 200,
    date: 2022-03-01T00:00:00.000Z,
    user: { _id: new ObjectId("..."), name: 'Charlie' }
  }
]

其他语言示例

类似的操作可以在其他编程语言中实现,如Python。以下是Python的示例代码:

安装PyMongo

在终端中运行以下命令来安装PyMongo:

pip install pymongo

插入数据

from pymongo import MongoClient
from datetime import datetime
def insert_data():
    client = MongoClient('mongodb://localhost:27017/')
    db = client['myDatabase']
    users_collection = db['users']
    orders_collection = db['orders']
    users_collection.delete_many({})  # 清空用户集合
    orders_collection.delete_many({})  # 清空订单集合
    # 插入用户数据
    users = users_collection.insert_many([
        { "name": "Alice" },
        { "name": "Bob" },
        { "name": "Charlie" }
    ])
    # 插入订单数据,并使用用户的ObjectId作为引用
    orders_collection.insert_many([
        { "userId": users.inserted_ids[0], "amount": 50.5, "date": datetime(2022, 1, 1) },
        { "userId": users.inserted_ids[0], "amount": 100.0, "date": datetime(2022, 2, 1) },
        { "userId": users.inserted_ids[1], "amount": 75.0, "date": datetime(2022, 1, 15) },
        { "userId": users.inserted_ids[2], "amount": 200.0, "date": datetime(2022, 3, 1) }
    ])
    print("Data inserted")
insert_data()

查询数据

def query_data():
    client = MongoClient('mongodb://localhost:27017/')
    db = client['myDatabase']
    users_collection = db['users']
    orders_collection = db['orders']
    # 查询某个用户及其所有订单
    print("\nQuery a user and their orders:")
    user = users_collection.find_one({ "name": "Alice" })
    orders = list(orders_collection.find({ "userId": user['_id'] }))
    print({ "user": user, "orders": orders })
    # 查询所有订单及其对应的用户信息
    print("\nQuery all orders and their corresponding users:")
    pipeline = [
        {
            '$lookup': {
                'from': 'users',  # 要关联的集合
                'localField': 'userId',  # orders 集合中的字段
                'foreignField': '_id',  # users 集合中的字段
                'as': 'user'  # 输出数组字段
            }
        },
        {
            '$unwind': '$user'  # 展开数组
        }
    ]
    results = list(orders_collection.aggregate(pipeline))
    for result in results:
        print(result)
query_data()

到此这篇关于MongoDB使用引用的示例的文章就介绍到这了,更多相关MongoDB使用引用内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 浅析Mongodb性能优化的相关问题

    浅析Mongodb性能优化的相关问题

    数据库性能对软件整体性能的影响是不言而喻的,那么,当我们使用MongoDB时改如何提高数据库性能呢?这篇文章通过范式化与反范式化、填充因子的使用和索引的使用三个方面来谈了谈Mongodb性能优化的相关问题,有需要的朋友们下面来一起看看吧。
    2016-10-10
  • Mongodb增加、移除Arbiter节点实例

    Mongodb增加、移除Arbiter节点实例

    这篇文章主要介绍了Mongodb增加、移除Arbiter节点实例,Arbiter是搭建Mongodb集群的一个必备节点,需要的朋友可以参考下
    2015-01-01
  • MongoDB 导出导入备份恢复数据详解及实例

    MongoDB 导出导入备份恢复数据详解及实例

    这篇文章主要介绍了MongoDB 导出导入备份恢复数据详解及实例的相关资料,需要的朋友可以参考下
    2016-10-10
  • MongoDB的启动方法详细总结

    MongoDB的启动方法详细总结

    MongoDB是一个基于分布式文件存储的数据库,下面这篇文章主要给大家介绍了关于MongoDB启动方法的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2023-03-03
  • MongoDB索引类型汇总分享

    MongoDB索引类型汇总分享

    这篇文章主要介绍了MongoDB索引类型汇总,单字段索引、复合索引、多键索引、文本索引、2dsphere索引等多种索引类型,需要的朋友可以参考一下
    2022-04-04
  • mongodb 命令行下及php中insert数据详解

    mongodb 命令行下及php中insert数据详解

    这篇文章主要介绍了mongodb 命令行下及php中insert数据详解,需要的朋友可以参考下
    2014-07-07
  • MongoDB修改、删除文档的域属性实例

    MongoDB修改、删除文档的域属性实例

    这篇文章主要介绍了MongoDB修改、删除文档的域属性实例,本文讲解了删除集合中所有文档的一个域、同时删除多个域、同时删除和新增域,需要的朋友可以参考下
    2015-02-02
  • 深入解析MongoDB中insert into select写法

    深入解析MongoDB中insert into select写法

    MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的,这篇文章主要介绍了MongoDB中insert into select写法,需要的朋友可以参考下
    2025-06-06
  • MongoDB常用数据库命令大全

    MongoDB常用数据库命令大全

    这篇文章主要介绍了MongoDB 常用数据库命令大全,需要的朋友可以参考下
    2020-02-02
  • MongoDB释放空闲空间的几种常用方法

    MongoDB释放空闲空间的几种常用方法

    这篇文章主要给大家介绍了关于MongoDB释放空闲空间的几种常用方法,分别包括compact、db.repairDatabase()、secondary节点重同步以及db.copyDatabase()这几种方法,需要的朋友可以参考借鉴,下面来一起看看吧
    2018-07-07

最新评论