Found 1661 Articles for Big Data Analytics

Find inside a hash MongoDB?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

278 Views

To find inside a hash MongoDB, you can use dot(.) notation. Let us first create a collection with documents> db.hashDemo.insertOne({"ClientName":"Larry", "ClientAge":23, "ClientDetails":{ "isEducated": true, "ClientProject" : "University Management"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1ef1266324ffac2a7dc5e") } > db.hashDemo.insertOne({"ClientName":"Chris", "ClientAge":26, "ClientDetails":{ "isEducated":false, "ClientProject" : "Online Book Store"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1ef7766324ffac2a7dc5f") }Following is the query to display all documents from a collection with the help of find() method> db.hashDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca1ef1266324ffac2a7dc5e"),    "ClientName" : "Larry",    "ClientAge" : 23,    "ClientDetails" : {       "isEducated" ... Read More

Query MongoDB for a datetime value less than NOW?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

709 Views

You can use $lte operator along with new Date() for this. Let us first create a collection with documents>db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Larry", "CustomerProductName":"Product-1", "ArrivalDate":new ISODate("2017-01-31")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1e8ab66324ffac2a7dc59") } >db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Mike", "CustomerProductName":"Product-2", "ArrivalDate":new ISODate("2019-04-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1e8c166324ffac2a7dc5a") } >db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Chris", "CustomerProductName":"Product-3", "ArrivalDate":new ISODate("2019-03-31")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1e8d266324ffac2a7dc5b") } >db.dateTimeValueLessThanNowDemo.insertOne({"CustomerName":"Robert", "CustomerProductName":"Product-4", "ArrivalDate":new ISODate("2019-04-02")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1e8e766324ffac2a7dc5c") }Following is the query to display all documents from a collection with the help of find() method> db.dateTimeValueLessThanNowDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca1e8c166324ffac2a7dc5a"), ... Read More

Adding new property to each document in a large MongoDB collection?

George John
Updated on 30-Jul-2019 22:30:25

832 Views

You can use update command along with forEach() for large collection. Let us first create a collection with documents>db.addingNewPropertyDemo.insertOne({"StudentName":"John", "StudentAge":23, "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1e61866324ffac2a7dc56") } >db.addingNewPropertyDemo.insertOne({"StudentName":"David", "StudentAge":21, "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1e62366324ffac2a7dc57") } >db.addingNewPropertyDemo.insertOne({"StudentName":"Bob", "StudentAge":21, "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1e62d66324ffac2a7dc58") }Following is the query to display all documents from a collection with the help of find() method> db.addingNewPropertyDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca1e61866324ffac2a7dc56"),    "StudentName" : "John",    "StudentAge" : 23,    "CountryName" : "US" } {    "_id" : ... Read More

How to terminate a MongoDB shell script earlier?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

506 Views

In order to terminate a MongoDB shell script earlier, you need to use quit. Following is the syntaxquit() quit(1)Let us create a script and try to write quit() or quit(1) in shell. At first, we will create the following collection with documents> db.flightInformation.insertOne({"FlightName":"Flight-1", "ArrivalTime":new ISODate("2019-03-12")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1e341e941bbb0e8bf5639") } > db.flightInformation.insertOne({"FlightName":"Flight-2", "ArrivalTime":new ISODate("2019-03-31")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca1e351e941bbb0e8bf563a") }Following is the query to display all documents from a collection with the help of find() method> db.flightInformation.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca1e341e941bbb0e8bf5639"),    "FlightName" : "Flight-1",   ... Read More

Renaming column name in a MongoDB collection?

George John
Updated on 30-Jul-2019 22:30:25

744 Views

To rename column name in a collection, you can use $rename operator. Following is the syntaxdb.yourCollectionName.update({}, {$rename: {'yourOldColumName': 'yourNewColumnName'}}, false, true);Let us first create a collection with documents:> db.renamingColumnNameDemo.insertOne({"StudentName":"Larry", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ee2c6d628fa4220163b9a") } > db.renamingColumnNameDemo.insertOne({"StudentName":"Sam", "Age":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ee2d0d628fa4220163b9b") } > db.renamingColumnNameDemo.insertOne({"StudentName":"Robert", "Age":27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ee2dbd628fa4220163b9c") }Following is the query to display all documents from a collection with the help of find() method> db.renamingColumnNameDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9ee2c6d628fa4220163b9a"),    "StudentName" : "Larry",    "Age" : 23 ... Read More

Get the average of an entire field using the aggregation framework in MongoDB?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

144 Views

You can use aggregate() method for this. Let us first create a collection with documents> db.averageAggregationDemo.insertOne({"PlayerGameScore":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ed66bd628fa4220163b95") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ed671d628fa4220163b96") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ed676d628fa4220163b97") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ed67bd628fa4220163b98") } > db.averageAggregationDemo.insertOne({"PlayerGameScore":16}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ed701d628fa4220163b99") }Following is the query to display all documents from a collection with the help of find() method> db.averageAggregationDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9ed66bd628fa4220163b95"), "PlayerGameScore" ... Read More

How to store query result (a single document) into a variable?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

552 Views

To store query result (a single document) into a variable, you can use var. Following is the syntaxvar anyVariableName=db.yourCollectionName.find().limit(1); yourVariableName; //Print the records;Let us first create a collection with documents> db.storeQueryResultDemo.insertOne({"ClientName":"Chris", "ClientAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ecf8fd628fa4220163b8d") } > db.storeQueryResultDemo.insertOne({"ClientName":"Robert", "ClientAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ecf97d628fa4220163b8e") } > db.storeQueryResultDemo.insertOne({"ClientName":"Sam", "ClientAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ecf9ed628fa4220163b8f") } > db.storeQueryResultDemo.insertOne({"ClientName":"Mike", "ClientAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ecfa8d628fa4220163b90") }Following is the query to display all documents from a collection with the help of find() method> db.storeQueryResultDemo.find().pretty();This will produce ... Read More

How to find two random documents in a MongoDB collection of 6?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

205 Views

Let us first create a collection and add some documents to it> db.twoRandomDocumentDemo.insertOne({"StudentId":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9aad628fa4220163b87") } > db.twoRandomDocumentDemo.insertOne({"StudentId":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9add628fa4220163b88") } > db.twoRandomDocumentDemo.insertOne({"StudentId":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9b0d628fa4220163b89") } > db.twoRandomDocumentDemo.insertOne({"StudentId":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9b3d628fa4220163b8a") } > db.twoRandomDocumentDemo.insertOne({"StudentId":5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9b7d628fa4220163b8b") } > db.twoRandomDocumentDemo.insertOne({"StudentId":7}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec9bad628fa4220163b8c") }Following is the query to display all documents from a collection with the help of find() method> db.twoRandomDocumentDemo.find();This ... Read More

How to check if field is a number in MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

538 Views

To check if field is a number in MongoDB, use the $type operator. Following is the syntaxdb.yourCollectionName.find({youtFieldName: {$type:"number"}}).pretty();Let us first create a collection with documents> db.checkIfFieldIsNumberDemo.insertOne({"StudentName":"John", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec75dd628fa4220163b83") } >db.checkIfFieldIsNumberDemo.insertOne({"StudentName":"Chris", "StudentMathScore":98, "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec77cd628fa4220163b84") } >db.checkIfFieldIsNumberDemo.insertOne({"StudentName":"Robert", "StudentCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec7a4d628fa4220163b85") } >db.checkIfFieldIsNumberDemo.insertOne({"StudentId":101, "StudentName":"Larry", "StudentCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9ec7ccd628fa4220163b86") }Following is the query to display all documents from a collection with the help of find() method> db.checkIfFieldIsNumberDemo.find().pretty();This will produce the following output{    "_id" : ... Read More

Building Multiple Indexes at once in MongoDB?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

2K+ Views

In order to build multiple indexes at once, you need to use createIndexes() and pass multiple keys into an array. Following is the query for building multiple indexes at once.>db.multipleIndexesDemo.createIndexes([{"First":1}, {"Second":1}, {"Third":1}, {"Fourth":1}, {"Fifth":1}]);This will produce the following output{    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 6,    "ok" : 1 }Now get all the indexes> db.multipleIndexesDemo.getIndexes();This will produce the following output[    {       "v" : 2,       "key" : {          "_id" : 1       },       "name" : "_id_",       ... Read More

Advertisements