Click the link: "MongoDB - Insert Document"
In this MongoDB tutorial we will learn to insert documents.
Insert one document
To insert one document into a collection we use the
db.collection.insertOne()
method.
Note!
collection
is the name of the collection in which you want to insert the document.
In the following example we are inserting one document into students collection.
> db.students.insertOne({
"firstname": "Yusuf",
"lastname": "Shakeel",
"studentid": "s01"
});
On success we will get a similar response in the terminal.
{
"acknowledged" : true,
"insertedId" : ObjectId("5d16c693e8cb73839ac9f2ec")
}
Note! insertedId is a unique id (primary key) given to each document in a collection by MongoDB.
To fetch the inserted documents in a collection we use the
find()
method.> db.students.find()
{ "_id" : ObjectId("5d16c693e8cb73839ac9f2ec"), "firstname" : "Yusuf", "lastname" : "Shakeel", "studentid" : "s01" }
We will learn more about
find()
in the Query Document tutorial.
No comments:
Post a Comment