MongoDB Basic Shell Commands (part-2)

MongoDB Basic Shell Commands (part-2)

MongoDB Basic Shell Commands (part-1)

Inserting New Documents

insert()

Af first, Connect to your Atlas Cluster. mongo "mongodb+srv://<username>:<password>@<cluster>.mongodb.net/admin" . By using the insert({document to be inserted}) method we can insert a new document inside a collection. Assuming that you have already switched to the database you are operating on, you can use the following command. Here the collection where the new document is being inserted is users, and the new document is passed as a argument to insert method. So the syntax is db.<collection>.insert(<document in json format>).

db.users.insert({
        "_id" : ObjectId("5c8eccc1caa187d17ca6ed16"),
        "city" : "ALPINE",
        "zip" : "35014",
        "loc" : {
                "y" : 33.331165,
                "x" : 86.208934
        },
        "pop" : 3062,
        "state" : "AL"
})

Inserting multiple documents by using an array

We can use the same insert() method to insert multiple documents, but this time you need to pass all the new documents inside an array like the following :

syntax : db.collection.insert([{<doc1>}, {<doc2>}]);

db.users.insert([{
        "city" : "ALPINE",
        "zip" : "35014",
        "loc" : {
                "y" : 33.331165,
                "x" : 86.208934
        },
        "pop" : 3062,
        "state" : "AL"
},
{
        "city" : "BESSEMER",
        "zip" : "35020",
        "loc" : {
                "y" : 33.409002,
                "x" : 86.947547
        },
        "pop" : 40549,
        "state" : "AL"
},
{
        "city" : "ACMAR",
        "zip" : "35004",
        "loc" : {
                "y" : 33.584132,
                "x" : 86.51557
        },
        "pop" : 6055,
        "state" : "AL"
}])

Here three documents are inserted in the users collection. "_id" field is mandatory, which will be autogenerated upon insertion and the documents will be saved like this :

{
        "_id" : ObjectId("5c8eccc1caa187d17ca6ed16"),
        "city" : "ALPINE",
        "zip" : "35014",
        "loc" : {
                "y" : 33.331165,
                "x" : 86.208934
        },
        "pop" : 3062,
        "state" : "AL"
},
{
        "_id" : ObjectId("5c8eccc1caa187d17ca6ed17"),
        "city" : "BESSEMER",
        "zip" : "35020",
        "loc" : {
                "y" : 33.409002,
                "x" : 86.947547
        },
        "pop" : 40549,
        "state" : "AL"
},
{
        "_id" : ObjectId("5c8eccc1caa187d17ca6ed18"),
        "city" : "ACMAR",
        "zip" : "35004",
        "loc" : {
                "y" : 33.584132,
                "x" : 86.51557
        },
        "pop" : 6055,
        "state" : "AL"
}

Here a second argument { "ordered": false } can be passed inside the insert() method. If not passed, its default value is true. By default documents will be inserted in order, if any error happens, insertion of the rest of the documents will be aborted. To prevent this default behavior you can pass this { "ordered": false } as a second argument, it will not abort the insertion operation for the rest of the documents if any error occurs. It looks like this :

db.users.insert([ { "test": 1 }, { "test": 2 }, { "test": 3 } ], {"ordered": false})

Updating Documents

updateOne() and updateMany()

db.collection.updateOne({<query>}, {<update>}) or db.collection.updateMany({<query>}, {<update>}) First argument of the following command is a query. It returns all the matched documents. So, it updates all documents in the users collection where the city field is equal to "HUDSON" by adding 10 to the current value of the "pop" field. $inc is an increment operator.

db.users.updateMany({ "city": "HUDSON" }, { "$inc": { "pop": 10 } })

The following command updates a single document which matched first with the query in the users collection where the zip field is equal to "12534" by setting the value of the "pop" field to 17630. Here $set is an operator for setting a value to a field.

db.users.updateOne({ "zip": "12534" }, { "$set": { "pop": 17630 } })

MongoDB Basic Shell Commands (part-3)

Thanks for reading, any correction or recommendation is welcome.