Found 1661 Articles for Big Data Analytics

Get Absolute value with MongoDB aggregation framework?

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

260 Views

You can use $abs operator for this. Let us first create a collection with documents> db.absoluteValueDemo.insert({"Value":98}); WriteResult({ "nInserted" : 1 }) > db.absoluteValueDemo.insert({"Value":-100}); WriteResult({ "nInserted" : 1 }) > db.absoluteValueDemo.insert({"Value":0}); WriteResult({ "nInserted" : 1 }) > db.absoluteValueDemo.insert({"Value":-9999990}); WriteResult({ "nInserted" : 1 })Following is the query to display all documents from a collection with the help of find() method:> db.absoluteValueDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5ca271f56304881c5ce84b9a"), "Value" : 98 } { "_id" : ObjectId("5ca271fa6304881c5ce84b9b"), "Value" : -100 } { "_id" : ObjectId("5ca271fe6304881c5ce84b9c"), "Value" : 0 } { "_id" : ObjectId("5ca2720f6304881c5ce84b9d"), "Value" : -9999990 }Following is the query to get ... Read More

How to query on top N rows in MongoDB?

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

3K+ Views

To query on top N rows in MongoDB, you can use aggregate framework. Let us create a collection with documents> db.topNRowsDemo.insertOne({"StudentName":"Larry", "Score":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca26eee6304881c5ce84b91") } > db.topNRowsDemo.insertOne({"StudentName":"Chris", "Score":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca26ef66304881c5ce84b92") } > db.topNRowsDemo.insertOne({"StudentName":"Mike", "Score":65}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca26efe6304881c5ce84b93") } > db.topNRowsDemo.insertOne({"StudentName":"Adam", "Score":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca26f066304881c5ce84b94") } > db.topNRowsDemo.insertOne({"StudentName":"John", "Score":86}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ca26f0f6304881c5ce84b95") }Following is the query to display all documents from a collection with the help of find() method> ... Read More

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

1K+ 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

174 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

263 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

806 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

599 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

803 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

Advertisements