Use Arrays as Filters by Querying Subdocuments in MongoDB

AmitDiwan
Updated on 03-Apr-2020 13:08:58

134 Views

For this, use $setIsSubset in MongoDB. Let us create a collection with documents −> db.demo407.insertOne( ...    { ...       Name:"Chris", ...       "details" : [ ...          { ...             id:100 ...          }, ...          { ...             id:110 ...          }, ...          { ...             id:130 ...          } ...       ] ...    } ... ); ... Read More

Create MongoDB User on Existing Database with Correct Role

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

265 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" ] ...    } ... )This will produce the following output −Successfully added user: {    "user" : "John",    "roles" : [       {          "role" : "readWrite",          "db" : "test"       }    ],    "mechanisms" : [       "SCRAM-SHA-256"    ] }Following is the query to show all users −> db.getUsers();This will produce the following output −[    {       "_id" : "test.John",       "user" : "John",       "db" : "test",       "roles" : [          {             "role" : "readWrite",             "db" : "test"          }       ],       "mechanisms" : [          "SCRAM-SHA-256"       ]    } ]

Aggregate Totals in One Group with MongoDB

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

154 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") } > db.demo406.insertOne({"Score":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f99defac4d418a017859c") } > db.demo406.insertOne({"Score":65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f99e3fac4d418a017859d") } > db.demo406.insertOne({"Score":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f99e6fac4d418a017859e") }Display all documents from a collection with the help of find() method −> db.demo406.find();This will ... Read More

Working with MongoDB Find

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

188 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"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f9134fac4d418a0178595") } > db.demo405.insertOne({"StudentInfo":{"Name":"David"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f9138fac4d418a0178596") } > db.demo405.insertOne({"StudentInfo":{"Name":"Bob"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f913cfac4d418a0178597") } > db.demo405.insertOne({"StudentInfo":{"Name":"John"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f9140fac4d418a0178598") }Display all documents from a ... Read More

Transaction Lock in MongoDB

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

653 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,    "insertedId" : ObjectId("5e6f8c40fac4d418a0178594") }Display all documents from a collection with the help of find() method −> db.demo404.find();This will produce the following output −{ "_id" : ObjectId("5e6f8c38fac4d418a0178592"), "FirstName" : "John" } { "_id" : ObjectId("5e6f8c3cfac4d418a0178593"), "FirstName" : "Robert" } { "_id" : ObjectId("5e6f8c40fac4d418a0178594"), "FirstName" : "Mike" }Following is the query ... Read More

MongoDB Query to Sort by Words

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

206 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" : ObjectId("5e287086cfb11e5c34d89931") }Display all documents from a collection with the help of find() method −> db.demo62.find();This will produce the following output −{ "_id" : ObjectId("5e287084cfb11e5c34d8992f"), "Subject" : "MySQL" } { "_id" : ObjectId("5e287085cfb11e5c34d89930"), "Subject" : "MongoDB" } { "_id" : ObjectId("5e287086cfb11e5c34d89931"), "Subject" : "Java" }Following is the query to sort by ... Read More

Sort on ObjectId Column in MongoDB

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") } > db.demo403.insertOne({"Name":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e6f89b8fac4d418a0178591") }Display all documents from a collection with the help of find() method −> db.demo403.find();This will produce the following output −{ "_id" : ObjectId("5e6f89b0fac4d418a017858e"), "Name" : "Chris" } { "_id" : ObjectId("5e6f89b2fac4d418a017858f"), "Name" : "David" } { "_id" : ObjectId("5e6f89b5fac4d418a0178590"), ... Read More

Get Intersection of Two Arrays in MongoDB

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

882 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 −> db.demo61.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e286e28cfb11e5c34d8992a"),    "Values1" : [       10,       20,       30,       40,       50    ],    "Values2" : [       30,       100, ... Read More

Conditional Update in MongoDB

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

954 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,    "insertedId" : ObjectId("5e612152fac4d418a0178587") }Display all documents from a collection with the help of find() method −> db.demo402.find();This will produce the following output −{ "_id" : ObjectId("5e61214efac4d418a0178585"), "id" : 101, "Name" : "Chris" } { "_id" : ObjectId("5e612150fac4d418a0178586"), "id" : 102, "Name" : "David" } { "_id" : ObjectId("5e612152fac4d418a0178587"), "id" : ... Read More

MongoDB Query to Get Date Records in a Range

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

383 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 05:11:20")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28642acfb11e5c34d89929") }Display all documents from a collection with the help of find() method −> db.demo60.find();This will produce the following output −{ "_id" : ObjectId("5e2863fecfb11e5c34d89927"), "ArrivalDate" : ISODate("2019-01-11T12:30:10Z") } { "_id" : ObjectId("5e28641acfb11e5c34d89928"), "ArrivalDate" : ISODate("2019-10-12T03:10:00Z") } { "_id" : ObjectId("5e28642acfb11e5c34d89929"), "ArrivalDate" ... Read More

Advertisements