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和安装MongoDB的方法,感兴趣的小伙伴们可以参考一下
    2016-07-07
  • centos7安装mongo数据库的方法(mongo4.2.8)

    centos7安装mongo数据库的方法(mongo4.2.8)

    这篇文章给大家介绍了centos7安装mongo4.2.8数据库的详细过程,包括mongo数据库安装和启动方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧
    2022-01-01
  • MongoDB增删查改操作示例【基于JavaScript Shell】

    MongoDB增删查改操作示例【基于JavaScript Shell】

    这篇文章主要介绍了MongoDB增删查改操作,结合实例形式分析了MongoDB数据库基于JavaScript Shell的基本增删查改操作技巧与使用注意事项,需要的朋友可以参考下
    2019-07-07
  • MongoDB中唯一索引(Unique)的那些事

    MongoDB中唯一索引(Unique)的那些事

    这篇文章主要给大家介绍了关于MongoDB中唯一索引(Unique)的那些事,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • MongoDB中的Primary Shard详解

    MongoDB中的Primary Shard详解

    在MongoDB的Sharding架构中,每个database中都可以存储两种类型的集合,一种是未分片的集合,一种是通过分片键,被打散的集合,下面给大家介绍MongoDB中的Primary Shard详解,感兴趣的朋友跟随小编一起看看吧
    2024-08-08
  • springboot整合mongodb

    springboot整合mongodb

    这篇文章主要介绍了springboot如何整合mongodb,mongodb的安装和使用,感兴趣的同学可以参考阅读本文
    2023-03-03
  • MongoDB系列教程(三):Windows中下载和安装MongoDB

    MongoDB系列教程(三):Windows中下载和安装MongoDB

    这篇文章主要介绍了MongoDB系列教程(三):MongoDB下载和安装,本文讲解使用Windows环境安装MongoDB,需要的朋友可以参考下
    2015-05-05
  • MongoDB用户管理授权方式

    MongoDB用户管理授权方式

    本文介绍了MongoDB角色类型及其授权模式,包括单个数据库、多个数据库、指定单个数据库等授权方式,并提供了授权命令示例,注意新用户需要在指定数据库中创建,并且授权模式影响登录串的指定方式
    2026-04-04
  • 浅谈MongoDB内部的存储原理

    浅谈MongoDB内部的存储原理

    这篇文章主要介绍了浅谈MongoDB内部的存储原理,MongoDB是一个面向文档的数据库系统。使用C++编写,不支持SQL,但有自己功能强大的查询语法,需要的朋友可以参考下
    2023-07-07
  • MongoDB下根据数组大小进行查询的方法

    MongoDB下根据数组大小进行查询的方法

    这篇文章主要介绍了MongoDB下根据数组大小进行查询的方法,分别实现了指定大小的数组和某个范围的数组,需要的朋友可以参考下
    2014-04-04

最新评论