Found 1661 Articles for Big Data Analytics

How to implement MongoDB $or query?

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

166 Views

The syntax is as follows for the $or query in MongoDB −db.yourCollectionName.find({ $or : [ { "yourFieldName" : "yourValue1" }, {"yourFieldName":"yourValue2"}, ...........N ] } ).pretty();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.orDemo.insertOne({"UserName":"Larry", "UserAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9491fd4cf1f7a64fa4df4c") } > db.orDemo.insertOne({"UserName":"David", "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9492074cf1f7a64fa4df4d") } > db.orDemo.insertOne({"UserName":"Mike", "UserAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c94920e4cf1f7a64fa4df4e") } > db.orDemo.insertOne({"UserName":"Sam", "UserAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9492144cf1f7a64fa4df4f") } ... Read More

How to update all documents in MongoDB?

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

834 Views

You can use updateMany() to update documents. Let us create a collection with a document. The query to create a collection with a document is as follows −> db.updateManyDocumentsDemo.insertOne({"StudentName":"John", "StudentLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c948edd4cf1f7a64fa4df48") } > db.updateManyDocumentsDemo.insertOne({"StudentName":"John", "StudentLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c948ee64cf1f7a64fa4df49") } > db.updateManyDocumentsDemo.insertOne({"StudentName":"Carol", "StudentLastName":"Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c948ef14cf1f7a64fa4df4a") } > db.updateManyDocumentsDemo.insertOne({"StudentName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c948f044cf1f7a64fa4df4b") }Display all documents from a collection with the help of find() method. The query is as follows −> db.updateManyDocumentsDemo.find().pretty();The following is the ... Read More

Is it possible to cast in a MongoDB Query?

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

324 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

267 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

250 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

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

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

544 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

Advertisements