Found 1659 Articles for Big Data Analytics

Is it possible to cast in a MongoDB Query?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

235 Views

Yes, it is possible to cast in a MongoDB query −db.yourCollectionName.find("this.yourFieldName >yourValue);To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.castingDemo.insertOne({"Amount":"200"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c947e874cf1f7a64fa4df42") } > db.castingDemo.insertOne({"Amount":"100"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c947e8e4cf1f7a64fa4df43") } > db.castingDemo.insertOne({"Amount":"110"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c947e944cf1f7a64fa4df44") } > db.castingDemo.insertOne({"Amount":"95"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c947e9d4cf1f7a64fa4df45") } > db.castingDemo.insertOne({"Amount":"85"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c947ea44cf1f7a64fa4df46") } > db.castingDemo.insertOne({"Amount":"75"}); {    "acknowledged" ... Read More

Match between fields in MongoDB aggregation framework?

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

182 Views

      You can use $cmp operator for this. To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.matchBetweenFieldsDemo.insertOne({"FirstValue":40, "SecondValue":70}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92c9625259fcd19549980d") } > db.matchBetweenFieldsDemo.insertOne({"FirstValue":20, "SecondValue":5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92c96b5259fcd19549980e") }Display all documents from a collection with the help of find() method. The query is as follows −> db.matchBetweenFieldsDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c92c9625259fcd19549980d"),    "FirstValue" : 40,    "SecondValue" : 70 } {    "_id" : ... Read More

How to get the equivalent for SELECT column1, column2 FROM tbl in MongoDB Database?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

179 Views

The equivalent syntax is as follows.db.yourCollectionName.find({}, {_id: 1, "column1": 1, "column2": 1}).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.equivalentForSelectColumn1Column2Demo.insertOne({"CustomerName":"John", "CustomerAge":26, "CustomerCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92c6205259fcd19549980a") } > db.equivalentForSelectColumn1Column2Demo.insertOne({"CustomerName":"David", "CustomerAge":22, "CustomerCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92c6305259fcd19549980b") } > db.equivalentForSelectColumn1Column2Demo.insertOne({"CustomerName":"Chris", "CustomerAge":24, "CustomerCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92c6415259fcd19549980c") }Display all documents from a collection with the help of find() method. The query is as follows −> db.equivalentForSelectColumn1Column2Demo.find().pretty();The following is the output ... Read More

MongoDB query by sub-field?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

1K+ Views

You can use dot(.) notation to query by subfield. Let us create a collection with a document. The query to create a collection with a document is as follows −> db.queryBySubFieldDemo.insertOne(    ... {       ... "StudentPersonalDetails" : {"StudentName" : "John", "StudentHobby" :"Photography"},       ... "StudentScores" : {"MathScore" : 56}    ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92c2995259fcd195499808") } > db.queryBySubFieldDemo.insertOne(    ... {       ... "StudentPersonalDetails" : {"StudentName" : "Chris", "StudentHobby" :"Reading"},       ... "StudentScores" : {"MathScore" : 97}    ... } ... ); { ... Read More

How can I check whether a field exists or not in MongoDB?

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

6K+ Views

To check whether a field exists or not in MongoDB, you can use the $exists operator.To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.checkFieldExistsOrNotDemo.insertOne({"StudentName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92ba4136de59bd9de063a1") } > db.checkFieldExistsOrNotDemo.insertOne({"StudentName":"John", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92ba4e36de59bd9de063a2") } > db.checkFieldExistsOrNotDemo.insertOne({"StudentName":"Chris", "StudentAge":24, "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92ba6536de59bd9de063a3") } > db.checkFieldExistsOrNotDemo.insertOne({"StudentName":"Robert", "StudentAge":21, "StudentCountryName":"UK", "StudentHobby":["Teaching", "Photography"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92ba9d36de59bd9de063a4") }Display all documents from a collection ... Read More

How to convert value to string using $toString in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

1K+ Views

Let us see an example to understand the $toString in MongoDB. To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.objectidToStringDemo.insertOne({"UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b80036de59bd9de0639d") } > db.objectidToStringDemo.insertOne({"UserName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b80436de59bd9de0639e") } > db.objectidToStringDemo.insertOne({"UserName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b80936de59bd9de0639f") } > db.objectidToStringDemo.insertOne({"UserName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b81836de59bd9de063a0") }Display all documents from a collection with the help of find() method. The query is as follows ... Read More

How to convert ObjectId to string in MongoDB

Smita Kapse
Updated on 30-Jul-2019 22:30:25

3K+ Views

To convert ObjectId to string, use the $toString in MongoDB. To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.objectidToStringDemo.insertOne({"UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b80036de59bd9de0639d") } > db.objectidToStringDemo.insertOne({"UserName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b80436de59bd9de0639e") } > db.objectidToStringDemo.insertOne({"UserName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b80936de59bd9de0639f") } > db.objectidToStringDemo.insertOne({"UserName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b81836de59bd9de063a0") }Display all documents from a collection with the help of find() method. The query is as follows −> ... Read More

How to hide _id from Aggregation?

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

441 Views

To hide _id from aggregation, use the below syntax −db.yourCollectionName.aggregate(    {$project : {       _id : 0 ,       yourIncludeFieldName:1,       yourIncludeFieldName:1    }} ).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.hideidDemo.insertOne({"UserName":"Larry", "UserAge":23, "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b02336de59bd9de06392") } > db.hideidDemo.insertOne({"UserName":"Chris", "UserAge":21, "UserCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b03036de59bd9de06393") } > db.hideidDemo.insertOne({"UserName":"Robert", "UserAge":26, "UserCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c92b04036de59bd9de06394") }Display all documents ... Read More

Is it possible to use MongoDB to query for entries that have a particular value in a field in an object in an array?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

51 Views

Yes, to query for a field in an object in the array with MongoDB, use the following syntax −db.yourCollectionName.find({"yourOuterFieldName": { $elemMatch: { "yourInnerFieldName": "yourValue" } } } ).pretty();To understand the above concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.objectInAnArrayDemo.insertOne({ "StudentDetails": [{    "StudentName": "John", "StudentMessage": "Hi"}, {"StudentName": "Larry", "StudentMessage": "Hello"}]}) {    "acknowledged" : true,    "insertedId" : ObjectId("5c92635d36de59bd9de06381") } > db.objectInAnArrayDemo.insertOne({ "StudentDetails": [{    "StudentName": "Carol", "StudentMessage": "Hello"}, {"StudentName": "David", "StudentMessage": "Good Morning"}]}) {    "acknowledged" : true,    "insertedId" : ObjectId("5c92637936de59bd9de06382") }Display all documents ... Read More

How to print to console an object in a MongoDB script?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

3K+ Views

You can use printjson() method to print to console an object in a MongoDB script. The syntax is as follows −printjson({yourFieldName”:yourValue”, ........N});You can use JSON.stringify() along with print() function. The syntax is as follows minus;print ( JSON.stringify( { {yourFieldName”:yourValue”, ........N} } ));Let us implement the above syntax to print object in Mongo script. The query is as follows −>printjson({"UserId":101, "UserName":"John", "UserCoreSuject":["Java", "MongoDB", "MySQL", "SQL Server"]});The following is the output −{    "UserId" : 101,    "UserName" : "John",    "UserCoreSuject" : [       "Java",       "MongoDB",       "MySQL",       "SQL Server"   ... Read More

Advertisements