Found 1349 Articles for MongoDB

To Aggregate Totals in One Group with MongoDB

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

87 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

114 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

525 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

141 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

How we can perform 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

How to get the intersection of two arrays in MongoDB?

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

632 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

MongoDB query to get date records in a range

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

316 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

How to do conditional update in MongoDB?

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

812 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

Set multiple conditions in MongoDB and fetch value in a range

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

116 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,    "insertedId" : ObjectId("5e28628fcfb11e5c34d89926") }Display all documents from a collection with the help of find() method −> db.demo59.find();This will produce the following output −{ "_id" : ObjectId("5e286286cfb11e5c34d89923"), "Values" : 50 } { "_id" : ObjectId("5e286288cfb11e5c34d89924"), "Values" : 10 } { "_id" : ObjectId("5e28628ccfb11e5c34d89925"), "Values" : 58 } { "_id" : ... Read More

How to update a MongoDB document without overwriting the existing one?

AmitDiwan
Updated on 03-Apr-2020 12:42:07

670 Views

To update only a field value, use update() along with $set. This won’t overwrite the existing one. Let us first create a collection with documents −> db.demo401.insertOne( ...    { ...       "_id" : 1001, ...       "Name" : "Chris", ...       "SubjectName" : "MongoDB", ...       "Score" : 45 ...    } ... ); { "acknowledged" : true, "insertedId" : 1001 }Display all documents from a collection with the help of find() method −> db.demo401.find();This will produce the following output −{ "_id" : 1001, "Name" : "Chris", "SubjectName" : "MongoDB", "Score" ... Read More

Advertisements