AmitDiwan has Published 10744 Articles

How to create MongoDB user on existing database with correct role?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:01:56

266 Views

To create a new user in MongoDB, use createUser(). Following is the query −> db.createUser( ...    { ...       user: "John", ...       pwd: "123456", ...       roles: [ { role: "readWrite", db: "test" } ], ...       mechanisms: [ "SCRAM-SHA-256" ... Read More

To Aggregate Totals in One Group with MongoDB

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:51:40

155 Views

To aggregate totals, use $sum in MongoDB. Let us create a collection with documents −> db.demo406.insertOne({"Score":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f99d5fac4d418a0178599") } > db.demo406.insertOne({"Score":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f99d8fac4d418a017859a") } > db.demo406.insertOne({"Score":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f99dcfac4d418a017859b") } ... Read More

Working with MongoDB find()

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:48:55

189 Views

The find() in MongoDB selects documents in a collection or view and returns a cursor to the selected documents.The find() method with no parameters returns all documents from a collection and returns all fields for the documents. Let us see an example and create a collection with documents −> db.demo405.insertOne({"StudentInfo":{"Name":"Chris"}}); ... Read More

Transaction lock in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:47:17

654 Views

Transaction support isn’t available in MongoDB 4.0. To get similar results, use findOneAndUpdate().Let us create a collection with documents −> db.demo404.insertOne({"FirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f8c38fac4d418a0178592") } > db.demo404.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f8c3cfac4d418a0178593") } > db.demo404.insertOne({"FirstName":"Mike"}); {    "acknowledged" : true, ... Read More

MongoDB query to sort by words

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:47:01

207 Views

To sort by words, use $addField along with $cond. Let us create a collection with documents −> db.demo62.insertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e287084cfb11e5c34d8992f") } > db.demo62.insertOne({"Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e287085cfb11e5c34d89930") } > db.demo62.insertOne({"Subject":"Java"}); {    "acknowledged" : true,    "insertedId" : ... Read More

How we can perform sort on ObjectId column in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:45:52

2K+ Views

To perform sort on ObjectId column, use sort(). Let us create a collection with document.> db.demo403.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f89b0fac4d418a017858e") } > db.demo403.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f89b2fac4d418a017858f") } > db.demo403.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f89b5fac4d418a0178590") } ... Read More

How to get the intersection of two arrays in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:45:10

885 Views

To get intersection of two arrays, use $setIntersection along with aggregate(). Let us create a collection with documents −> db.demo61.insertOne({"Values1":[10, 20, 30, 40, 50], "Values2":[30, 100, 70, 120, 40]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e286e28cfb11e5c34d8992a") }Display all documents from a collection with the help of find() method ... Read More

How to do conditional update in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:44:30

955 Views

Use update() for conditional update in MongoDB. Let us first create a collection with documents −> db.demo402.insertOne({id:101, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e61214efac4d418a0178585") } > db.demo402.insertOne({id:102, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e612150fac4d418a0178586") } > db.demo402.insertOne({id:103, "Name":"Mike"}); {    "acknowledged" : true,   ... Read More

MongoDB query to get date records in a range

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:43:41

384 Views

To get date records in a range, use $gt along with $lt. Let us create a collection with documents −> db.demo60.insertOne({"ArrivalDate":new ISODate("2019-01-11 12:30:10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2863fecfb11e5c34d89927") } > db.demo60.insertOne({"ArrivalDate":new ISODate("2019-10-12 03:10:00")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28641acfb11e5c34d89928") } > db.demo60.insertOne({"ArrivalDate":new ISODate("2019-01-14 ... Read More

Set multiple conditions in MongoDB and fetch value in a range

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:42:28

163 Views

Let us create a collection with documents −> db.demo59.insertOne({"Values":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e286286cfb11e5c34d89923") } > db.demo59.insertOne({"Values":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e286288cfb11e5c34d89924") } > db.demo59.insertOne({"Values":58}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28628ccfb11e5c34d89925") } > db.demo59.insertOne({"Values":78}); {    "acknowledged" : true, ... Read More

Advertisements