mongodb 文档的嵌入和引用
mongodb 是介于关系型与非关系型数据之间的,mongodb的join查询可以通过引用来实现。可以将文档内容嵌入到另一个文档中,也可以将文档内容引用到另一个文档中。嵌入意味着要把某一类型的数据,如包含更多数据的数组,嵌入到文档本身。引用意味着创建一个引用
mongodb 是介于关系型与非关系型数据之间的,mongodb的join查询可以通过引用来实现。 可以将文档内容嵌入到另一个文档中,也可以将文档内容引用到另一个文档中。 嵌入意味着要把某一类型的数据,如包含更多数据的数组,嵌入到文档本身。 引用意味着创建一个引用,包含另一个文档的数据。相当于关系型数据库。 一. 嵌入 例如:我想使用一个关系型数据库来记录CD、DVD和购买信息。在这个数据中,需要一个表来收集CD,另一个表来存储CD歌曲信息。因此,如果要查询特定的信息就可能需要查询多个表的。 对于mongodb 或者其他非关系型数据库,会更容易的嵌入这些信息,采用这种方法会使数据库简洁,确保所有相关信息保存在单一的文档中,同时,检索数据更快。 数据结构如下: 关系型|_media |_cds |_id, artist, title, genre, releasedate |_ cd_tracklists |_cd_id, songtitle, length
|_media |_items |_
[ { "_id" : ObjectId("5353463193efef02c962da73"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells like teen spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] } ]
> use ttlsa_com switched to db ttlsa_com > apress = ( { "_id" : "Apress", "Type" : "Technical Publisher", "Category" : ["IT", "Software","Programming"] } ) { "_id" : "Apress", "Type" : "Technical Publisher", "Category" : [ "IT", "Software", "Programming" ] } > db.publisherscollection.insert(apress)
> book = ( { "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress","Author" : ["Membrey,Peter","Plugge, Eelco","Hawkins, Tim"] } ) { "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress", "Author" : [ "Membrey,Peter", "Plugge, Eelco", "Hawkins, Tim" ] } > db.mediaCollection.insert(book)
> book = db.mediaCollection.findOne({"ISBN" : "987-1-4302-3051-9"}) { "Author" : [ "Hawkins, Tim", "Plugge, Eelco" ], "ISBN" : "987-1-4302-3051-9", "Publisher" : "Apress", "Title" : " Different Title 2", "Type" : "Book", "_id" : ObjectId("5353462f93efef02c962da71") } > book.Publisher Apress > db.publisherscollection.findOne( { _id : book.Publisher } ) { "_id" : "Apress", "Type" : "Technical Publisher", "Category" : [ "IT", "Software", "Programming" ] }
> apress = ( { "Type" : "Technical Publisher", "Category" :["IT","Software","Programming"] } ) { "Type" : "Technical Publisher", "Category" : [ "IT", "Software", "Programming" ] } > db.publisherscollection.save(apress) > apress { "Type" : "Technical Publisher", "Category" : [ "IT", "Software", "Programming" ], "_id" : ObjectId("53588c221697e7511678752c") } > book = { "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Author": ["Membrey, Peter","Plugge,Eelco","Hawkins, Tim"], Publisher : [ new DBRef ('publisherscollection',apress._id) ] } { "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Author" : [ "Membrey, Peter", "Plugge,Eelco", "Hawkins, Tim" ], "Publisher" : [ DBRef("publisherscollection", ObjectId("53588c221697e7511678752c")) ] } > db.media.save(book) > db.media.find().toArray() [ { "_id" : ObjectId("53588ce01697e7511678752d"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB, the", "ISBN" : "987-1-4302-3051-9", "Author" : [ "Membrey, Peter", "Plugge,Eelco", "Hawkins, Tim" ], "Publisher" : [ DBRef("publisherscollection", ObjectId("53588c221697e7511678752c")) ] } ]
原文地址:mongodb 文档的嵌入和引用, 感谢原作者分享。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

It is recommended to use the latest version of MongoDB (currently 5.0) as it provides the latest features and improvements. When selecting a version, you need to consider functional requirements, compatibility, stability, and community support. For example, the latest version has features such as transactions and aggregation pipeline optimization. Make sure the version is compatible with the application. For production environments, choose the long-term support version. The latest version has more active community support.

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

The data of the MongoDB database is stored in the specified data directory, which can be located in the local file system, network file system or cloud storage. The specific location is as follows: Local file system: The default path is Linux/macOS:/data/db, Windows: C:\data\db. Network file system: The path depends on the file system. Cloud Storage: The path is determined by the cloud storage provider.

The MongoDB database is known for its flexibility, scalability, and high performance. Its advantages include: a document data model that allows data to be stored in a flexible and unstructured way. Horizontal scalability to multiple servers via sharding. Query flexibility, supporting complex queries and aggregation operations. Data replication and fault tolerance ensure data redundancy and high availability. JSON support for easy integration with front-end applications. High performance for fast response even when processing large amounts of data. Open source, customizable and free to use.

MongoDB is a document-oriented, distributed database system used to store and manage large amounts of structured and unstructured data. Its core concepts include document storage and distribution, and its main features include dynamic schema, indexing, aggregation, map-reduce and replication. It is widely used in content management systems, e-commerce platforms, social media websites, IoT applications, and mobile application development.

The benefits of functions returning reference types in C++ include: Performance improvements: Passing by reference avoids object copying, thus saving memory and time. Direct modification: The caller can directly modify the returned reference object without reassigning it. Code simplicity: Passing by reference simplifies the code and requires no additional assignment operations.

The MongoDB database file is located in the MongoDB data directory, which is /data/db by default, which contains .bson (document data), ns (collection information), journal (write operation records), wiredTiger (data when using the WiredTiger storage engine ) and config (database configuration information) and other files.

On Linux/macOS: Create the data directory and start the "mongod" service. On Windows: Create the data directory and start the MongoDB service from Service Manager. In Docker: Run the "docker run" command. On other platforms: Please consult the MongoDB documentation. Verification method: Run the "mongo" command to connect and view the server version.
