Found 1659 Articles for Big Data Analytics

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

671 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

How to query MongoDB with a LIMIT?

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

131 Views

To query MongoDB with limit, use LIMIT() method. Let us create a collection with documents −> db.demo58.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f8fcfb11e5c34d8991f") } > db.demo58.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f93cfb11e5c34d89920") } > db.demo58.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f94cfb11e5c34d89921") } > db.demo58.insertOne({"Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f99cfb11e5c34d89922") }Display all documents from a collection with the help of find() method −> db.demo58.find();This will produce the following output −{ "_id" : ObjectId("5e285f8fcfb11e5c34d8991f"), "Name" : "David" } { "_id" : ObjectId("5e285f93cfb11e5c34d89920"), "Name" : "David" } { "_id" : ... Read More

MongoDB query to update only a single item from voting (up and down) records?

AmitDiwan
Updated on 03-Apr-2020 12:39:49

75 Views

Let us first create a collection with documents −> db.demo57.insertOne({"Votes":{"VoterName":"Chris", "TotalVote":50}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285bb8cfb11e5c34d8991a") } > db.demo57.insertOne({"Votes":{"VoterName":"David", "TotalVote":101}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285bc3cfb11e5c34d8991b") }Display all documents from a collection with the help of find() method −> db.demo57.find();This will produce the following output −{ "_id" : ObjectId("5e285bb8cfb11e5c34d8991a"), "Votes" : { "VoterName" : "Chris", "TotalVote" : 50 } } { "_id" : ObjectId("5e285bc3cfb11e5c34d8991b"), "Votes" : { "VoterName" : "David", "TotalVote" : 101 } }Here is the query to update only a single item (TotalVote) −> db.demo57.update({"Votes.VoterName":"David" }, { $inc : { "Votes.TotalVote" ... Read More

How to efficiently run complex queries on MongoDB unindexed fields?

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

205 Views

Create an index to efficiently run complex queries. Let us first create a collection with documents −> db.demo400.insertOne({SubjectName:"Java Spring"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610720fac4d418a0178572") } > db.demo400.insertOne({SubjectName:"Spring Hibernate"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e61072dfac4d418a0178573") } > db.demo400.insertOne({SubjectName:"Java Hibernate"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610736fac4d418a0178574") } > db.demo400.createIndex({SubjectName:"text"}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Display all documents from a collection with the help of find() method −> db.demo400.find();This will produce the following output −{ "_id" : ObjectId("5e610720fac4d418a0178572"), "SubjectName" : "Java ... Read More

MongoDB query to remove a specific document

AmitDiwan
Updated on 03-Apr-2020 12:39:02

131 Views

To remove a specific document, use remove() in MongoDB. Let us create a collection with documents −> db.demo56.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e272e0bcfb11e5c34d89917") } > db.demo56.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e272e10cfb11e5c34d89918") } > db.demo56.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e272e13cfb11e5c34d89919") }Display all documents from a collection with the help of find() method −> db.demo56.find();This will produce the following output −{ "_id" : ObjectId("5e272e0bcfb11e5c34d89917"), "Name" : "Chris" } { "_id" : ObjectId("5e272e10cfb11e5c34d89918"), "Name" : "David" } { "_id" : ObjectId("5e272e13cfb11e5c34d89919"), "Name" : "Bob" }Following is the query to remove a ... Read More

Advertisements