Found 1659 Articles for Big Data Analytics

How do we print a variable at the MongoDB command prompt?

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

2K+ Views

In order to print a variable at the MongoDB command prompt, use the following syntax//Declaring and Initializing a variable. var anyVariableName=yourValue; //To print the above variable. yourVariableName; Or print(yourVariableName);Following is how you can declare and initialize a variable at the MongoDB command prompt> var myIntegerValue=20;Print a variable at the MongoDB command prompt:> myIntegerValueThis will produce the following output20You can also use print(). Following is the query> print(myIntegerValue);This will produce the following output20Let us see another example. Following is the query to declare and initialize a string variable at MongoDB shell.> var myStringValue="Hello MongoDB!!!";Following is the query to print a variable ... Read More

How do I find all documents with a field that is NaN in MongoDB?

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

763 Views

To find all documents with a field that is NAN in MongoDB, use the following syntaxdb.yourCollectionName.find( { yourFieldName: NaN })Let us first create a collection with documents> db.nanDemo.insertOne({"Score":0/0}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca251a26304881c5ce84b8a") } > db.nanDemo.insertOne({"Score":10/5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2520e6304881c5ce84b8b") } > db.nanDemo.insertOne({"Score":20/0}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca252156304881c5ce84b8c") } > db.nanDemo.insertOne({"Score":0/20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2521e6304881c5ce84b8d") }Following is the query to display all documents from a collection with the help of find() method> db.nanDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca251a26304881c5ce84b8a"), "Score" ... Read More

Set max on MongoDB capped collection?

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

126 Views

Capped collections are fixed-size collections that support high-throughput operations that insert and retrieve documents based on insertion order. In order to set max on mongo capped collection, use the following syntaxdb.createCollection("yourCollectionName", {capped:true, size:yourSizeValue, max:yourMaxValue});Let us implement the above syntax in order to set max on mongo capped collection. Following is the query> db.createCollection("setMaxDemo", {capped:true, size:948575757, max:2000});This will produce the following output{ "ok" : 1 }You can now get all information about the above collection> db.setMaxDemo.stats();This will produce the following output{    "ns" : "test.setMaxDemo",    "size" : 0,    "count" : 0,    "storageSize" : 4096,    "capped" : true, ... Read More

How can I use $elemMatch on first level array in MongoDB?

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

158 Views

You can use $in operator instead of $elemMatch on first level array. The syntax is as followsdb.yourCollectionName.find({yourFieldName:{$in:["yourValue"]}}).pretty();Let us first create a collection with documents>db.firstLevelArrayDemo.insertOne({"StudentName":"Chris", "StudentTechnicalSkills":["Mongo DB", "MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2360f66324ffac2a7dc71") } >db.firstLevelArrayDemo.insertOne({"StudentName":"Robert", "StudentTechnicalSkills":["C", "J ava", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2362766324ffac2a7dc72") }Following is the query to display all documents from a collection with the help of find() method> db.firstLevelArrayDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca2360f66324ffac2a7dc71"),    "StudentName" : "Chris",    "StudentTechnicalSkills" : [       "MongoDB",       "MySQL",       "SQL ... Read More

Retrieving the first document in a MongoDB collection?

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

3K+ Views

To retrieve the first document in a collection, you can use findOne(). Following is the syntaxvar anyVariableName=db.yourCollectionName.findOne(); //To print result at MongoDB console write the variable name yourVariableNameLet us first create a collection with documents> db.retrieveFirstDocumentDemo.insertOne({"ClientName":"Robert", "ClientAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2325966324ffac2a7dc6d") } > db.retrieveFirstDocumentDemo.insertOne({"ClientName":"Chris", "ClientAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2326266324ffac2a7dc6e") } > db.retrieveFirstDocumentDemo.insertOne({"ClientName":"Larry", "ClientAge":29}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2326a66324ffac2a7dc6f") } > db.retrieveFirstDocumentDemo.insertOne({"ClientName":"David", "ClientAge":39}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2327566324ffac2a7dc70") }Following is the query to display all documents from a collection with the help of ... Read More

Using find() to search for nested keys in MongoDB?

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

669 Views

For find() to search for nested keys in MongoDB, you can use dot(.) notation. Following is the syntaxdb.yourCollectionName.find({"yourOuterFieldName.yourInnerFieldName":"yourValue"}).pretty();Let us first create a collection with documents:>db.searchForNestedKeysDemo.insertOne({"ClientName":"Larry", "ClientAge":28, "ClientExtra Details":{"isEducated":true, "CountryName":"US"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca20e8b66324ffac2a7dc64") } >db.searchForNestedKeysDemo.insertOne({"ClientName":"Chris", "ClientAge":29, "ClientExtra Details":{"isEducated":false, "CountryName":"UK"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca20ea366324ffac2a7dc65") } >db.searchForNestedKeysDemo.insertOne({"ClientName":"David", "ClientAge":39, "ClientExtra Details":{"isEducated":true, "CountryName":"AUS"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca20eba66324ffac2a7dc66") }Following is the query to display all documents from a collection with the help of find() method> db.searchForNestedKeysDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca20e8b66324ffac2a7dc64"),    "ClientName" : "Larry",   ... Read More

How to prevent MongoDB from returning the object ID while finding a document?

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

480 Views

To prevent MongoDB from returning the Object ID while finding a document, you need to set _id to 0. Let us first create a collection with documents> db.preventObjectIdDemo.insertOne( ...    { ... ...       "StudentName" : "Chris", ...       "StudentDetails" : [ ...          { ...             "StudentTotalScore" : 540, ...             "StudentCountryName" : "US" ...          }, ...          { ...             "StudentTotalScore" : 489, ...           ... Read More

How to convert from string to date data type in MongoDB?

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

669 Views

To convert from String to date data type, you need to write some script. Let us first create a collection with documents>db.stringToDateDataTypeDemo.insertOne({"CustomerName":"Carol", "ShippingDate":"2019- 01-21"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2071d66324ffac2a7dc60") } >db.stringToDateDataTypeDemo.insertOne({"CustomerName":"Bob", "ShippingDate":"2019- 02-24"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2073566324ffac2a7dc61") } >db.stringToDateDataTypeDemo.insertOne({"CustomerName":"Chris", "ShippingDate":"2019- 04-01"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca2074266324ffac2a7dc62") }Following is the query to display all documents from a collection with the help of find() method> db.stringToDateDataTypeDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5ca2071d66324ffac2a7dc60"),    "CustomerName" : "Carol",    "ShippingDate" : "2019-01-21" } {    "_id" : ObjectId("5ca2073566324ffac2a7dc61"), ... Read More

Find inside a hash MongoDB?

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

220 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

584 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

Advertisements