Getting Set Up
Kindly read the main aide in this series for more data about setting up MongoDB - Installation, Shell, and Database Engineering.
Bulk Insertion
That's right, you read that right. Like MySQL, you can likewise embed numerous records at a solitary point on schedule. A similar supplement() work takes protests as well as clusters as information. This time, we will be apathetic and make something straightforward for the following two records.
db.students.insert([
{
name: "User1",
degree: "Engineering",
email: "user1@example.com"
},
{
name: "User 2",
degree: "Engineering",
email: "user2@example.com",
phone: ["2589636988"]
}
]);
You can see that the above records are both unique. This leads us to the greatest benefit of Document Databases. You needn't bother with a fixed construction for the records and each record can be any legitimate JavaScript articulation.
Evolving pattern "on the fly" is the greatest benefit of utilizing NoSQL.
Presently, we should take a stab at executing the above embed inquiry on our data set.
> db.students.insert(
... [
... {
... "name": "User1",
... "degree": "Engineering",
... "email": "user1@example.com"
... },
... {
... "name": "User 2",
... "degree": "Engineering",
... "email": "user2@example.com",
... "phone": ["2589636988"]
... },
... ]
... );
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
>
You get the yield of the BulkWriteResult work, where it says nothing for mistakes and we have a check of '2' for the embedded records. How about we rapidly check our table substance by utilizing find() strategy.
> db.students.find();
{ "_id" : ObjectId("592ebe7e8e61243307417cc4"), "name" : "Praveen Kumar", "degree" : "Cloud Computing", "email" : "praveen@example.com", "subjects" : [ { "name" : "Internet Networks", "prof" : "Prof. Awesome Blossom" }, { "name" : "Cloud Computing", "prof" : "Prof. Tech Ninja" }, { "name" : "Web Development", "prof" : "Prof. Chunky Monkey" } ], "phone" : [ "123456780", "9967728336", "7772844242" ] }
{ "_id" : ObjectId("592ed5818e61243307417cc5"), "name" : "User1", "degree" : "Engineering", "email" : "user1@example.com" }
{ "_id" : ObjectId("592ed5818e61243307417cc6"), "name" : "User 2", "degree" : "Engineering", "email" : "user2@example.com", "phone" : [ "2589636988" ] }
>
We got the three records and each has distinctive construction, which is extraordinary! Since there are an excessive number of fields, how about we make it pretty():
> db.students.find().pretty();
{
"_id" : ObjectId("592ebe7e8e61243307417cc4"),
"name" : "Praveen Kumar",
"degree" : "Cloud Computing",
"email" : "praveen@example.com",
"subjects" : [
{
"name" : "Internet Networks",
"prof" : "Prof. Awesome Blossom"
},
{
"name" : "Cloud Computing",
"prof" : "Prof. Tech Ninja"
},
{
"name" : "Web Development",
"prof" : "Prof. Chunky Monkey"
}
],
"phone" : [
"123456780",
"9967728336",
"7772844242"
]
}
{
"_id" : ObjectId("592ed5818e61243307417cc5"),
"name" : "User1",
"degree" : "Engineering",
"email" : "user1@example.com"
}
{
"_id" : ObjectId("592ed5818e61243307417cc6"),
"name" : "User 2",
"degree" : "Engineering",
"email" : "user2@example.com",
"phone" : [
"2589636988"
]
}
>
Querying Records
For questioning or sifting the fields, we need to pass them as boundary objects in the find() work. One model will be, we should check whether I can get the record with Meaow:
> db.students.find({"name" : "User 2"});
{ "_id" : ObjectId("592ed5818e61243307417cc6"), "name" : "User 2", "degree" : "Engineering", "email" : "user2@example.com", "phone" : [ "2589636988" ] }
>
Also, not surprisingly, our pretty() would return:
> db.students.find({"name" : "User 2"}).pretty();
{
"_id" : ObjectId("592ed5818e61243307417cc6"),
"name" : "User 2",
"degree" : "Engineering",
"email" : "user2@example.com",
"phone" : [
"2589636988"
]
}
>
This additionally works for things inside exhibits. The find() technique claims matches of the qualities here. Here is one more model for coordinating with a number inside an exhibit:
> db.students.find({"phone": "123456780"}).pretty();
{
"_id" : ObjectId("592ebe7e8e61243307417cc4"),
"name" : "Praveen Kumar",
"degree" : "Cloud Computing",
"email" : "praveen@example.com",
"subjects" : [
{
"name" : "Internet Networks",
"prof" : "Prof. Awesome Blossom"
},
{
"name" : "Cloud Computing",
"prof" : "Prof. Tech Ninja"
},
{
"name" : "Web Development",
"prof" : "Prof. Chunky Monkey"
}
],
"phone" : [
"123456780",
"9967728336",
"7772844242"
]
}
>
The above order effectively distinguishes my record and shows it, despite the fact that 123456780 is just one of three numbers present.
Updating Records
The technique we use here is db.collection.update(). It takes up two boundaries and the principal boundary is an object of a key worth pair for a match that is available in the records. The following boundary is the substance that the principal boundary should be supplanted with.
So how about we take a stab at something like this:
db.students.update(
{ name: "Praveen Kumar" },
{
name: "Praveen Kumar",
degree: "Cloud Computing MSc",
email: "praveen@example.net",
subjects: [
{
name: "Internet Networks",
prof: "Prof. Awesome Blossom"
},
{
name: "Cloud Computing",
prof: "Prof. Tech Ninja"
},
{
name: "Web Development",
prof: "Prof. Chunky Monkey"
}
],
phone: ["123456780", "9967728336", "7772844242"]
}
);
- Note 1: You need to give the full article if there should be an occurrence of update work, since it makes a supplanting of the entire record with the subsequent boundary. In the event that we didn't set different qualities, it will simply have one record with only two of the things: the email and the degree.
- Note 2: Do not find by name, as I just did, assuming there is any chance of this happening. There might be numerous records coordinating with a similar boundary, so use something remarkable like _id.
Attempting the abovementioned, we get:
> db.students.update({"name": "Praveen Kumar"}, {
... "name": "Praveen Kumar",
... "degree": "Cloud Computing MSc",
... "email": "praveen@example.net",
... "subjects": [
... {
... "name": "Internet Networks",
... "prof": "Prof. Awesome Blossom"
... },
... {
... "name": "Cloud Computing",
... "prof": "Prof. Tech Ninja"
... },
... {
... "name": "Web Development",
... "prof": "Prof. Chunky Monkey"
... }
... ],
... "phone": ["123456780", "9967728336", "7772844242"]
... });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
Presto! We should see the outcomes:
{
"nMatched": 1,
"nUpserted": 0,
"nModified": 1
}
It coordinated with one record and it changed it. There's none upserted (we'll cover this later). This implies that there's plausible that it may coordinate, however not update. How about we run a similar order again and see what occurs.
Gracious dear! It shows a similar yield. Perhaps I was over-energetic.
To perceive what has transformed, we could attempt to run the find() work alongside our pretty pretty() work.
> db.students.find({"phone": "123456780"}).pretty();
{
"_id" : ObjectId("592ebe7e8e61243307417cc4"),
"name" : "Praveen Kumar",
"degree" : "Cloud Computing MSc",
"email" : "praveen@example.net",
"subjects" : [
{
"name" : "Internet Networks",
"prof" : "Prof. Awesome Blossom"
},
{
"name" : "Cloud Computing",
"prof" : "Prof. Tech Ninja"
},
{
"name" : "Web Development",
"prof" : "Prof. Chunky Monkey"
}
],
"phone" : [
"123456780",
"9967728336",
"7772844242"
]
}
>
Utilizing '$set'
It's a torment to add the entire record once more, just to transform one single worth. The uplifting news is, there's a way around that: utilizing the $set administrator. Let's assume we need to change my email to praveen@example.net. All that we require to do is:
db.students.update(
{
name: "Praveen Kumar"
},
{
$set: {
email: "praveen@example.net"
}
}
);
Quick and painless! Presently how about we see the yield of the above order.
> db.students.update({
... "name": "Praveen Kumar"
... }, {
... $set: {
... "email" : "praveen@example.net"
... }
... });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
This $set is to some degree like our SQL's UPDATE inquiry. At the point when the question is terminated, it keeps the all around existing worth flawless and updates the particular fields. Like UPDATE, $set shows the number of columns the question has influenced.
On the off chance that we see the above outcome, we can track down that the order has coordinated with one and adjusted one, yet not upserted any. On the off chance that we attempt to run a similar order once more, it will give the accompanying outcome:
WriteResult({ nMatched: 1, nUpserted: 0, nModified: 0 });
Fantastic! Presently we know see that coordinated and altered have various tallies. This implies, regardless of whether you continue to send a similar order for update without the $set administrator, the adjustment continues to happen constantly, while the $set administrator gets it going just in case there are various qualities.
Note: $set is more execution proficient, in case you are making a great deal of updates.
Increasing Numeric Values
There's another administrator that helps us increase numeric qualities. Consider a record that has some numeric boundary, for example, a field called focuses. focuses are incredible on the grounds that they need to augment much of the time. Being apathetic, I will utilize the $set work:
db.students.update(
{
name: "Praveen Kumar"
},
{
$set: {
points: 15
}
}
);
Executing the above order, we get this:
> db.students.update({
... "name": "Praveen Kumar"
... }, {
... $set: {
... "points" : 15
... }
... });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
>
Also, checking in the event that it has been refreshed, we fire out the find() order and we get:
> db.students.find({"phone": "123456780"}).pretty();
{
"_id" : ObjectId("592ebe7e8e61243307417cc4"),
"name" : "Praveen Kumar",
"degree" : "Cloud Computing MSc",
"email" : "praveen@example.com",
"subjects" : [
{
"name" : "Internet Networks",
"prof" : "Prof. Awesome Blossom"
},
{
"name" : "Cloud Computing",
"prof" : "Prof. Tech Ninja"
},
{
"name" : "Web Development",
"prof" : "Prof. Chunky Monkey"
}
],
"phone" : [
"123456780",
"9967728336",
"7772844242"
],
"points" : 15
}
>
Fantastic. We have a focuses field in my record with a worth of 15.
That whole cycle ought to be clear at this point. In the case of something is hazy, glance back at the past segment of the aide as well as shoot an inquiry for extra clearness.
Continuing on, I complete this article, subsequently acquiring 5 additional focuses. This condition can be created with one little change:
db.students.update(
{
name: "Praveen Kumar"
},
{
$inc: {
points: 5
}
}
);
As should be obvious, changing $set to $inc, has the effect. This is like utilizing the increased task administrator += in standard dialects, instead of the task administrator =.
> db.students.update({
... "name": "Praveen Kumar"
... }, {
... $inc: {
... "points" : 5
... }
... });
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find({"phone": "123456780"}).pretty();
{
"_id" : ObjectId("592ebe7e8e61243307417cc4"),
"name" : "Praveen Kumar",
"degree" : "Cloud Computing MSc",
"email" : "praveen@example.com",
"subjects" : [
{
"name" : "Internet Networks",
"prof" : "Prof. Awesome Blossom"
},
{
"name" : "Cloud Computing",
"prof" : "Prof. Tech Ninja"
},
{
"name" : "Web Development",
"prof" : "Prof. Chunky Monkey"
}
],
"phone" : [
"123456780",
"9967728336",
"7772844242"
],
"points" : 20
}
>
Goodness, that was a simple augmentation for me from 15 to 20. Obviously, utilizing $inc is an incredible method to increase numeric qualities.