mongodb简介

tech2023-11-25  73

mongodb简介

As a PHP developer you are probably used to seeing applications and articles using MySQL or some other relational database management system (RDBMS). But in the past few years, a new kind of database has gained adoption in the software development community. This new type of database focuses on document objects rather than strictly defined records and relationships, and has been nicknamed “NoSQL.”

作为PHP开发人员,您可能习惯于使用MySQL或其他关系数据库管理系统(RDBMS)查看应用程序和文章。 但是在过去的几年中,一种新型的数据库已在软件开发社区中得到采用。 这种新型的数据库专注于文档对象,而不是严格定义的记录和关系,并且被昵称为“ NoSQL”。

There are a lot of implementations of the NoSQL concept, but one of the most famous and widely used NoSQL databases is MongoDB. I think it’s one of the most interesting NoSQL databases available currently, and it’s considered by many to be one of the easiest to use (which has helped it gain widespread adoption).

NoSQL概念的实现很多,但是最著名和使用最广泛的NoSQL数据库之一是MongoDB。 我认为它是当前可用的最有趣的NoSQL数据库之一,并且被许多人认为是最易于使用的数据库之一(这已帮助它获得了广泛的采用)。

In this article I’ll introduce you to NoSQL with MongoDB. You’ll learn how to install the MongoDB extension for PHP, and how to add, update, and retrieve document objects. If you’re used to working with RDBMSs like MySQL or PostgreSQL, you’ll find some of the concepts of working with MongoDB a bit strange, but you’ll soon grow to love the flexibility and power that MongoDB gives you!

在本文中,我将向您介绍MongoDB的NoSQL。 您将学习如何为PHP安装MongoDB扩展,以及如何添加,更新和检索文档对象。 如果您曾经使用过MySQL或PostgreSQL等RDBMS,将​​会发现使用MongoDB的一些概念有些奇怪,但是您很快就会喜欢上MongoDB所提供的灵活性和强大功能了!

关于MongoDB (About MongoDB)

MongoDB is a document-oriented database and each document has its own structure. Unlike a RDBMS in which each record must conform to the structure of its table, each document in MongoDB can have a different structure; you don’t have to define a schema for documents before saving them in the database.

MongoDB是一个面向文档的数据库,每个文档都有其自己的结构。 与RDBMS中的每个记录都必须符合其表的结构不同,MongoDB中的每个文档可以具有不同的结构。 您无需在将文档保存到数据库中之前为其定义架构。

MongoDB groups document objects into collections. You can think of a collection as a table like you would create in a RDBMS, but the difference as I said before is that they won’t force you to define a schema before you can store something.

MongoDB将文档对象分组为集合 。 您可以将集合视为类似于在RDBMS中创建的表,但是正如我之前所说的,不同之处在于它们不会强迫您在存储内容之前定义架构。

With MongoDB, you can embed a document inside another one, which is really useful for cases where there is a one-to-one relationship. In a typical RDBMS you’d need to create two tables and link them together with a foreign key to achieve the same result. MongoDB doesn’t support joins, which some people see as a con. But if you organize your data correctly then you’ll find you don’t need joins, which is a pro since you’ll benefit from very high performance.

使用MongoDB,您可以将一个文档嵌入另一个文档中,这对于存在一对一关系的情况非常有用。 在典型的RDBMS中,您需要创建两个表,并使用外键将它们链接在一起以获得相同的结果。 MongoDB不支持联接,有些人认为联接是不利的。 但是,如果您正确地组织数据,则会发现您不需要连接,这是一个专家,因为您将受益于非常高的性能。

It’s worth mentioning the aim of MongoDB and NoSQL isn’t to kill off RDBMS. RDBMSs are still a very good solution for most of the development world’s needs, but they do have their weaknesses, most noticeably the need to define a rigid schema for your data which is one problem NoSQL tries to solve.

值得一提的是,MongoDB和NoSQL的目标并不是杀死RDBMS。 RDBMS仍然是满足大多数开发世界需求的很好的解决方案,但是它们确实有缺点,最明显的是需要为您的数据定义一个严格的模式,这是NoSQL试图解决的问题。

安装 (Installation)

MongoDB is easy to set up. You can download the compressed file of the latest version from its website and unpack it on your server. Then, create a directory for the database and run the mongod binary with the path to your database directory.

MongoDB易于设置。 您可以从其网站下载最新版本的压缩文件,然后将其解压缩到服务器上。 然后,为数据库创建目录,然后运行mongod二进制文件以及数据库目录的路径。

shreef@indigo:~$ tar zxf mongodb-<version>.tgz shreef@indigo:~$ mv mongodb-<version> mongodb shreef@indigo:~$ mkdir mongodb/data shreef@indigo:~$ mongodb/bin/mongod --dbpath mongodb/data &

Then, you’ll need to install the MongoDB extension for PHP from PECL.

然后,您需要从PECL安装适用于PHP的MongoDB扩展 。

shreef@indigo:~$ sudo pecl install mongo

Enable the extension in your php.ini and restart Apache if necessary.

在php.ini启用扩展名,并在必要时重新启动Apache。

extension=mongo.so

使用MongoDB (Using MongoDB)

Using PHP’s MongoDB extension is easy. I’ll show you how to perform the basic operations you’ll use the most, and highlight some common pitfalls you may come across.

使用PHP的MongoDB扩展很容易。 我将向您展示如何执行您最常使用的基本操作,并突出显示您可能会遇到的一些常见陷阱。

连接中 (Connecting)

The first thing you’ll need is to establish a connection to the MongoDB server, which is as simple as creating a new instance of the Mongo class. By default, the new Mongo object will try to connect to the MongoDB server running on your localhost using port 27017. You can change this by passing a connection string when you instantiate the object.

您需要做的第一件事就是建立与MongoDB服务器的连接,就像创建一个Mongo类的新实例一样简单。 默认情况下,新的Mongo对象将尝试使用端口27017连接到在本地主机上运行的MongoDB服务器。您可以在实例化该对象时通过传递连接字符串来进行更改。

<?php // connects to localhost on port 27017 by default $mongo = new Mongo(); // connects to 192.168.25.190 on port 50100 $mongo = new Mongo("mongodb://192.168.25.190:50100");

Next you’ll want to select the name of the database you’re going to use. If you want to access the database named blog then just select it as if it were a property of $mongo.

接下来,您将要选择要使用的数据库的名称。 如果要访问名为blog的数据库,则只需选择它,就好像它是$mongo的属性一样。

<?php $db = $mongo->blog;

$db now holds a MongoDB object representing the blog database.

$db现在拥有一个表示博客数据库的MongoDB对象。

Selecting the collection that you want to save your objects in is very similar to how you selected the database; select the collection posts by accessing it as a property of the MongoDB object.

选择要用于保存对象的集合与选择数据库的方式非常相似。 通过访问作为的属性选择集合帖 MongoDB对象。

<?php $collection = $db->posts;

$collection now holds a MongoCollection object representing the posts collection.

$collection现在包含一个表示posts集合的MongoCollection对象。

If no database or collection exists with the name you’ve provided, MongoDB will create a new one with that name once you insert your first document object. Alternatively, you can force MongoDB to create the collection earlier using the createCollection() method.

如果不存在使用您提供的名称的数据库或集合,则在您插入第一个文档对象后,MongoDB将使用该名称创建一个新的数据库或集合。 另外,您可以强制MongoDB使用createCollection()方法更早地创建集合。

<?php $collection = $db->createCollection("posts");

插入文件 (Inserting Documents)

To insert a new object into the posts collection you use the MongoCollection object’s insert() method, passing in an array of data to be saved.

要将新对象插入posts集合,请使用MongoCollection对象的insert()方法,传入要保存的数据数组。

<?php $document = array( "title" => "cat with a hat", "content" => "once upon a time a cat with a hat ..."); $collection->insert($document);

You can see that $document is just a simple associative array here. After you call insert(), the array is modified with the new key _id and a value that is a MongoId object instance having been added.

您可以在这里看到$document只是一个简单的关联数组。 调用insert() ,将使用新键_id修改数组,并添加一个值为MongoId对象实例的值。

Array ( [title] => cat with a hat [content] => once upon a time a cat with a hat ... [_id] => MongoId Object ( [$id] => 4ea2213af7ede43c53000000 ) )

_id is the primary key for the document and must be unique to each document object in the collection. Generally it’s a good idea to let MongoDB assign the key since then you can be sure there won’t be any collisions.

_id是文档的主键,并且对于集合中的每个文档对象都必须是唯一的。 通常,让MongoDB分配密钥是一个好主意,因为这样您就可以确保不会发生任何冲突。

If you try to use the same array again with insert() you’ll receive a MongoCursorException since the ID already exists already in the collection. Go ahead and try to insert the same document twice…

如果您尝试通过insert()再次使用相同的数组,您将收到MongoCursorException因为该ID已经存在于集合中。 继续尝试两次插入相同的文档…

<?php $document = array( "title" => "cat with a hat", "content" => "once upon a time a cat with a hat ..."); $collection->insert($document); $collection->insert($document);

Hrm, what’s that? No exception was thrown?

嗯,那是什么? 没有抛出异常吗?

You didn’t get an error because you are not doing a safe insert. The MongoDB extension for PHP performs all operations asynchronously by default so you don’t have to wait until the server responds that the data was successfully saved. This feature lets you move on to the next task without waiting for confirmation whether the data was saved or not and can be very convenient, but for important data you may want to be sure the document object was really saved and no errors happened. You can achieve this by passing an array as the second parameter of insert() with the key safe set to true.

您没有收到错误消息,因为您没有在进行安全插入 。 默认情况下,适用于PHP的MongoDB扩展异步执行所有操作,因此您不必等到服务器响应成功保存了数据。 此功能使您可以继续执行下一个任务,而无需等待确认是否保存了数据,这非常方便,但是对于重要数据,您可能需要确保文档对象确实已保存并且没有发生错误。 您可以通过将数组作为insert()的第二个参数insert()密钥安全设置为true insert()来实现。

Try it again, but this time pass the second parameter to insert().

再试一次,但这一次将第二个参数传递给insert() 。

<?php $insertOpts = array("safe" => true); $collection->insert($document, $insertOpts); $collection->insert($document, $insertOpts);

Now you’ll get the exception I mentioned earlier since you are performing a safe insert and another object with the same ID already exists in the collection.

现在,您将得到前面提到的异常,因为您正在执行安全插入,并且集合中已经存在另一个具有相同ID的对象。

更新文件 (Updating Documents)

If you want to modify an existing document object, you can use the save() method.

如果要修改现有文档对象,则可以使用save()方法。

<?php $document["author"] = "Shreef"; $collection->save($document);

If the passed array has no _id key, the document will be inserted and a primary key will be assigned. If the array already has an _id key then the document object will be updated. Like insert(), save() also accepts an optional second parameter.

如果传递的数组没有_id键,则将插入文档并分配主键。 如果数组已具有_id键,则将更新文档对象。 与insert() , save()也接受可选的第二个参数。

The save() method is not the only way to update an existing object; you can also use the update() method. update() takes the criteria of the object you want to update as the first parameter, and the updated object as the second parameter.

save()方法不是更新现有对象的唯一方法。 您还可以使用update()方法。 update()将要更新的对象的条件作为第一个参数,将更新的对象作为第二个参数。

Let’s say I want to find all objects whose author key equals “Shreef” and update the value to “Timothy”. I might try the following:

假设我要查找所有作者键等于“ Shreef”的对象,并将其值更新为“ Timothy”。 我可以尝试以下方法:

<?php $collection->update( array("author" => "Shreef"), array("author" => "Timothy"));

While this works, it also has the unintended consequence of removing all other fields in the matching documents; the only fields they would have would be author! This isn’t what I wanted. To update only the value of the author key and leave the other key/values untouched, you need to use the $set modifier.

虽然这样做有效,但也会带来意外结果,即删除匹配文档中的所有其他字段; 他们仅有的领域是作者 ! 这不是我想要的。 要仅更新作者键的值,而保持其他键/值不变,则需要使用$set修饰符。

<?php $collection->update( array("author" => "Shreef"), array('$set' => array("author" => "Timothy")));

But this still isn’t exactly what I wanted since MongoDB will only update the first matching object it finds. To update all the matching document objects, you need to pass an array as a third parameter to update() with the key multiple set to true.

但这仍然不是我想要的,因为MongoDB只会更新它找到的第一个匹配对象。 要更新所有匹配的文档对象,您需要将数组作为第三个参数传递给update()且其键的倍数设置为true。

<?php $collection->update( array("author" => "Shreef"), array('$set' => array("author" => "Timothy")), array("multiple" => true));

Finally I’m able to update all the matching records how I wanted. The $set modifier ensures only the value of the author key is updated, and the multiple key tells MongoDB to update every matching document object it finds.

最终,我能够根据需要更新所有匹配的记录。 $set修饰符可确保仅更新作者密钥的值,而多重密钥则指示MongoDB更新找到的每个匹配文档对象。

选择文件 (Selecting Documents)

To select all the stored documents from a collection that match some criteria, you use the find() method. The method takes the criteria of your query as the first parameter, and optionally as a second parameter it can take an array of the field names to return instead of the entire object.

要从集合中选择符合某些条件的所有存储文档,请使用find()方法。 该方法将查询的条件用作第一个参数,可选地将其作为第二个参数,它可以使用要返回的字段名称数组而不是整个对象。

<?php $cursor = $collection->find(array("author" => "shreef")); foreach ($cursor as $document) { print_r($document); }

find() returns a MongoCursor object that you can use to iterate through the results.

find()返回一个MongoCursor对象,可用于遍历结果。

If you only want to retrieve one value, you can use the findOne() method which will return only an array with the fields of the first matching object.

如果只想检索一个值,则可以使用findOne()方法,该方法将仅返回具有第一个匹配对象的字段的数组。

<?php $document = $collection->findOne(array("author" => "shreef")); print_r($document);

摘要 (Summary)

MongoDB is a very simple yet powerful database that you can use if you have large amounts of unstructured data. There are many, many more features than what I have mentioned in this article, and maybe I’ll write another article discussing some of them in the future. Until then, try to follow the examples mentioned in the article and feel free to leave comments with any questions you have.

MongoDB是一个非常简单但功能强大的数据库,如果您有大量的非结构化数据,则可以使用。 除了我在本文中提到的功能外,还有许多其他功能,也许将来我会写另一篇文章讨论其中的一些功能。 在此之前,请尝试遵循本文中提到的示例,并随时对您有任何问题发表评论。

Image via graph / Shutterstock

图片通过图形 / Shutterstock

翻译自: https://www.sitepoint.com/introduction-to-mongodb/

mongodb简介

最新回复(0)