CRUD operations refer to the basic Insert, Read, Update and Delete operations. In the previous chapter, we learnt about how to create a database and drop the database in MongoDB. Now, let us learn how to perform CRUD (Create/Read/Update/Delete) operations in MongoDB.
CRUD操作是指基本的插入,读取,更新和删除操作。 在上一章中,我们了解了如何创建数据库并将数据库删除到MongoDB中。 现在,让我们学习如何在MongoDB中执行CRUD(创建/读取/更新/删除)操作。
MongoDB:将文档插入集合(创建) (MongoDB: Inserting a document into a collection(Create))
The command db.collection.insert()
will perform an insert operation into a collection of a document.
命令db.collection.insert()
将执行插入操作到文档集合中。
Let us insert a document to a student
collection. You must be connected to a database for doing any insert. It is done as follows:
让我们将文档插入到student
集合中。 您必须连接到数据库才能进行任何插入。 可以这样完成:
db.student.insert({
regNo: "3014",
name: "Test Student",
course: {
courseName: "MCA",
duration: "3 Years"
},
address: {
city: "Bangalore",
state: "KA",
country: "India"
}
})
Note that an entry has been made into the collection called student.
请注意,已将一个名为“ student”的条目输入到集合中。
MongoDB:从集合中查询文档(阅读) (MongoDB: Querying a document from a collection(Read))
To retrieve (Select) the inserted document, run the below command. The find()
command will retrieve all the documents of the given collection.
要检索(选择)插入的文档,请运行以下命令。 find()
命令将检索给定集合的所有文档。
db.collection_name.find()
NOTE: Please observe that the record retrieved contains an attribute called _id
with some unique identifier value called ObjectId which acts as a document identifier.
注意:请注意,检索到的记录包含一个名为_id
的属性,该属性具有一些称为ObjectId的唯一标识符值,该值充当文档标识符。
If a record is to be retrieved based on some criteria, the find()
method should be called passing parameters, then the record will be retrieved based on the attributes specified.
如果要根据某些条件检索记录,则应将find()
方法称为传递参数,然后将根据指定的属性检索记录。
db.collection_name.find({"fieldname":"value"})
For Example: Let us retrieve the record from the student collection where the attribute regNo is 3014 and the query for the same is as shown below:
例如:让我们从学生集合中检索记录,其中regNo属性为3014 , 对它的查询如下所示:
db.students.find({"regNo":"3014"})
MongoDB:更新集合中的文档(更新) (MongoDB: Updating a document in a collection(Update))
In order to update specific field values of a collection in MongoDB, run the below query.
为了更新MongoDB中集合的特定字段值,请运行以下查询。
db.collection_name.update()
update()
method specified above will take the fieldname and the new value as argument to update a document.
上面指定的update()
方法将以字段名和新值作为参数来更新文档。
Let us update the attribute name of the collection student for the document with regNo 3014.
让我们使用regNo 3014更新文档的收款学生的属性名称 。
db.student.update({
"regNo": "3014"
},
$set:
{
"name":"Viraj"
})
You will see the following in the Command Prompt:
您将在命令提示符中看到以下内容:
MongoDB:从集合中删除条目(删除) (MongoDB: Removing an entry from the collection(Delete))
Let us now look into the deleting an entry from a collection. In order to delete an entry from a collection, run the command as shown below:
现在让我们研究一下从集合中删除条目。 为了从集合中删除条目,请运行以下命令:
db.collection_name.remove({"fieldname":"value"})
For Example: db.student.remove({"regNo":"3014"})
例如: db.student.remove({"regNo":"3014"})
Note that after running the remove()
method, the entry has been deleted from the student collection.
请注意,在运行remove()
方法之后,该条目已从学生集合中删除。
翻译自: https://www.studytonight.com/mongodb/crud-operations-mongodb