Found 1659 Articles for Big Data Analytics

How to sort by the difference in array contents in MongoDB?

AmitDiwan
Updated on 01-Apr-2020 11:33:27

130 Views

To sort by difference, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo155.insertOne({"Scores":[{"Value":45}, {"Value":50}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e354584fdf09dd6d08539e3") } > db.demo155.insertOne({"Scores":[{"Value":60}, {"Value":10}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e35458efdf09dd6d08539e4") } > db.demo155.insertOne({"Scores":[{"Value":100}, {"Value":95}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e354599fdf09dd6d08539e5") }Display all documents from a collection with the help of find() method −> db.demo155.find();This will produce the following output −{ "_id" : ObjectId("5e354584fdf09dd6d08539e3"), "Scores" : [ { "Value" : 45 }, { "Value" : 50 } ] } { "_id" : ObjectId("5e35458efdf09dd6d08539e4"), "Scores" : [ { "Value" ... Read More

Query a nested field within an array with MongoDB

AmitDiwan
Updated on 01-Apr-2020 11:06:59

258 Views

To query a nested field within an array, use $elemMatch in MongoDB. Let us create a collection with documents −> db.demo153.insertOne({"ClientDetails":[{"ClientName":"Chris", "ClientProject":"Online Banking System"}, {"ClientName":"David", "ClientProject":"Online School Management"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e351957fdf09dd6d08539df") } > db.demo153.insertOne({"ClientDetails":[{"ClientName":"Carol", "ClientProject":"Online Book System"}, {"ClientName":"Mike", "ClientProject":"Game Development"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3519c9fdf09dd6d08539e0") }Display all documents from a collection with the help of find() method −> db.demo153.find();This will produce the following output −{    "_id" : ObjectId("5e351957fdf09dd6d08539df"), "ClientDetails" : [       { "ClientName" : "Chris", "ClientProject" : "Online Banking System" },       { "ClientName" ... Read More

MongoDB query to update an array using FindAndUpdate()?

AmitDiwan
Updated on 01-Apr-2020 11:04:38

315 Views

To update an array, instead use findAndModify() in MongoDB. Let us create a collection with documents −> db.demo152.insertOne({"id":102, "Name":["Chris", "David"], Score:45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3515bcfdf09dd6d08539dd") } > db.demo152.insertOne({"id":103, "Name":["Mike", "Carol"], Score:65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3515cafdf09dd6d08539de") }Display all documents from a collection with the help of find() method −> db.demo152.find();This will produce the following output −{ "_id" : ObjectId("5e3515bcfdf09dd6d08539dd"), "id" : 102, "Name" : [ "Chris", "David" ], "Score" : 45 } { "_id" : ObjectId("5e3515cafdf09dd6d08539de"), "id" : 103, "Name" : [ "Mike", "Carol" ], "Score" : 65 }Following is the ... Read More

MongoDB projection result as an array of selected items?

AmitDiwan
Updated on 01-Apr-2020 11:02:17

101 Views

Let us create a collection with documents −> db.demo151.insertOne({"ListOfNames":["Chris", "David", "Mike"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3513b6fdf09dd6d08539da") } > db.demo151.insertOne({"ListOfNames":["Mike", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3513c4fdf09dd6d08539db") } > db.demo151.insertOne({"ListOfNames":["John", "David", "Chris"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3513dcfdf09dd6d08539dc") }Display all documents from a collection with the help of find() method −> db.demo151.find();This will produce the following output −{ "_id" : ObjectId("5e3513b6fdf09dd6d08539da"), "ListOfNames" : [ "Chris", "David", "Mike" ] } { "_id" : ObjectId("5e3513c4fdf09dd6d08539db"), "ListOfNames" : [ "Mike", "Bob" ] } { "_id" : ObjectId("5e3513dcfdf09dd6d08539dc"), "ListOfNames" : [ "John", "David", "Chris" ] ... Read More

Conditional update depending on field matched in MongoDB

AmitDiwan
Updated on 01-Apr-2020 10:58:57

388 Views

For conditional update, use update() and set new value using $set. Let us create a collection with documents −> db.demo150.insertOne({"StudentId":101, "StudentName":"Chris", "StudentMarks":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350dcdfdf09dd6d08539d3") } > db.demo150.insertOne({"StudentId":102, "StudentName":"Chris", "StudentMarks":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350dcefdf09dd6d08539d4") } > db.demo150.insertOne({"StudentId":103, "StudentName":"David", "StudentMarks":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350dcffdf09dd6d08539d5") } > db.demo150.insertOne({"StudentId":104, "StudentName":"Chris", "StudentMarks":38}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e350dd0fdf09dd6d08539d6") }Display all documents from a collection with the help of find() method −> db.demo150.find();This will produce the following output −{ "_id" : ObjectId("5e350dcdfdf09dd6d08539d3"), "StudentId" : 101, "StudentName" ... Read More

How to make a case-insensitive query in MongoDB?

AmitDiwan
Updated on 01-Apr-2020 07:53:42

232 Views

For a case-insensitive query, use regex in MongoDB. Let us create a collection with documents −> db.demo314.insertOne({"Name":"Chris brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50d742f8647eb59e562056") } > db.demo314.insertOne({"Name":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50d743f8647eb59e562057") } > db.demo314.insertOne({"Name":"CHRIS BROWN"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50d744f8647eb59e562058") } > db.demo314.insertOne({"Name":"DAVID MILLER"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50d747f8647eb59e562059") } > db.demo314.insertOne({"Name":"chris brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50d749f8647eb59e56205a") }Display all documents from a collection with the help of find() method −> db.demo314.find();This will produce the following output −{ "_id" ... Read More

MongoDB $elemMatch to match document

AmitDiwan
Updated on 01-Apr-2020 07:51:20

203 Views

Let us create a collection with documents −> db.demo313.insertOne({"_id":100, "details":[{"Name":"Chris", "Age":24}]}); { "acknowledged" : true, "insertedId" : 100 } > db.demo313.insertOne({"_id":101, "details":[{"Name":"David", "Age":22}]}); { "acknowledged" : true, "insertedId" : 101 } > db.demo313.insertOne({"_id":102, "details":[{"Name":"Mike", "Age":25}]}); { "acknowledged" : true, "insertedId" : 102 }Display all documents from a collection with the help of find() method −> db.demo313.find();This will produce the following output −{ "_id" : 100, "details" : [ { "Name" : "Chris", "Age" : 24 } ] } { "_id" : 101, "details" : [ { "Name" : "David", "Age" : 22 } ] } { "_id" : 102, "details" ... Read More

MongoDB - update partial number of documents?

AmitDiwan
Updated on 01-Apr-2020 07:47:24

121 Views

To update partial number of documents, set multi to true. Let us create a collection with documents −> db.demo312.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ce16f8647eb59e56204a") } > db.demo312.insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ce19f8647eb59e56204b") } > db.demo312.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ce1cf8647eb59e56204c") } > db.demo312.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ce20f8647eb59e56204d") } > db.demo312.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50ce22f8647eb59e56204e") }Display all documents from a collection with the help of find() method −> db.demo312.find();This will produce the following output −{ "_id" : ObjectId("5e50ce16f8647eb59e56204a"), "FirstName" ... Read More

Get the count of a specific value in MongoDB quickly

AmitDiwan
Updated on 01-Apr-2020 07:45:50

221 Views

For faster queries, create an index. To get the count, use count(). Let us create a collection with documents −> db.demo311.ensureIndex({"Name":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo311.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50cd01f8647eb59e562044") } > db.demo311.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50cd05f8647eb59e562045") } > db.demo311.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50cd06f8647eb59e562046") } > db.demo311.insertOne({"Name":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e50cd0af8647eb59e562047") } > db.demo311.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50cd0df8647eb59e562048") } > db.demo311.insertOne({"Name":"Chris"}); { ... Read More

MongoDB query to push document into an array

AmitDiwan
Updated on 01-Apr-2020 07:43:36

332 Views

To push document into an array, use $push along with update(). Let us create a collection with documents −>db.demo310.insertOne({"Name":"Chris", "details":[{"Id":101, "Subject":"MySQL"}, {"Id":102, "Subject":"MongoDB"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e50cabdf8647eb59e562043") }Display all documents from a collection with the help of find() method −> db.demo310.find();This will produce the following output −{    "_id" : ObjectId("5e50cabdf8647eb59e562043"), "Name" : "Chris", "details" : [       { "Id" : 101, "Subject" : "MySQL" }, { "Id" : 102, "Subject" : "MongoDB" }    ] }Following is the query to push document −> db.demo310.update({ _id:ObjectId("5e50cabdf8647eb59e562043")}, ...{ $push: {"details": { ...   "Id" ... Read More

Advertisements