Found 1659 Articles for Big Data Analytics

MongoDB pull with positional operator?

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

92 Views

Use $pull operator along with positional operator($) in MongoDB. Let us first create a collection with documents −> db.pullWithPositionalOperatorDemo.insertOne( ...   { ...      _id: 100, ...      "StudentDetails": [ ...         { ...            "StudentId": "STU-1", ...            "StudentFavouriteSubject": ["MongoDB", "Java"] ...         }, ...         { ...            "StudentId": "STU-2", ...            "StudentFavouriteSubject": ["PHP", "MySQL"] ...         } ...      ] ...   } ... ); { ... Read More

Search a sub-field on MongoDB?

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

235 Views

To search a sub-filed in MongoDB, you can use double quotes along with dot notation. Let us first create a collection with documents −> db.searchSubFieldDemo.insertOne( ...   { ...      "UserDetails": ...      {"UserEmailId":"John123@gmail.com", "UserAge":21} ...   } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3d909edc6604c74817ce2") } > db.searchSubFieldDemo.insertOne( { "UserDetails": {"UserEmailId":"Carol@yahoo.com", "UserAge":26} } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3d9a4edc6604c74817ce3") }Following is the query to display all documents from a collection with the help of find() method −> db.searchSubFieldDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd3d909edc6604c74817ce2"),    "UserDetails" ... Read More

How to define aliases in the MongoDB Shell?

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

146 Views

To define aliases in MongoDB shell, you can use below syntax −Object.defineProperty(this, 'yourFunctionName', {    get: function() {       yourStatement1,       .       .       return N    },    enumerable: true,    configurable: true });Following is the syntax to assign with var −var anyAliasName=yourFunctionName;Let us implement the above syntax in order to define an aliases in the MongoDB shell. Here, 'displayMessageDemo' is our function −> Object.defineProperty(this, 'displayMessageDemo', { ...   get: function() { ...      return "Hello MongoDB" ...   }, ...   enumerable: true, ...   configurable: true ... });Query to assign function to var in MongoDB shell −> var myMessage = displayMessageDemo;Let us display the value of above aliases −> myMessage;This will produce the following output −Hello MongoDB

How to check if a field in MongoDB is [] or {}?

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

115 Views

To check if a field in MongoDB is [] or {}, you can use the following syntax −db.yourCollectionName.find({    "yourOuterFieldName": { "$gt": {} },    "yourOuterFieldName.0": { "$exists": false } });Let us first create a collection with documents -> db.checkFieldDemo.insert([ ...   { _id: 1010, StudentDetails: {} }, ...   { _id: 1011, StudentDetails: [ { StudentId: 1 } ] }, ...   { _id: 1012, StudentDetails: [ {} ] }, ...   { _id: 1013 }, ...   { _id: 1014, StudentDetails: null}, ...   { _id: 1015, StudentDetails: { StudentId: 1 } } ... ]); BulkWriteResult({    "writeErrors" ... Read More

How to get all collections where collection name like '%2015%'?

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

432 Views

Let us first create some collections that starts from year number i.e. 2015, 2019, etc −> use web; switched to db web > db.createCollection("2015-myCollection"); { "ok" : 1 } > db.createCollection("2019-employeeCollection"); { "ok" : 1 } > db.createCollection("2015-yourCollection"); { "ok" : 1 }Now you can display all the collections with the help of SHOW −> show collections;This will produce the following output −2015-myCollection 2015-yourCollection 2019-employeeCollection applyConditionDemo check creatingAliasDemo emp_info emptyCollection removeNullDemoFollowing is the query to get all collections where collection name like ‘%2015%’ −> db.getCollectionNames().filter(function (v) { return /^2015\-/.test(v); })This will produce the following output −[ "2015-myCollection", "2015-yourCollection" ]If you ... Read More

MongoDB query to fetch elements between a range excluding both the numbers used to set range?

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

1K+ Views

Let’s say both the numbers are 50 and 60. You can use below syntax −db.yourCollectionName.find({yourFieldName: { $gt : 50 , $lt : 60 } } );If you want to include 50 and 60 also then use below syntax −db.yourCollectionName.find({yourFieldName: { $gte : 50 , $lte : 60 } } );Let us first create a collection with documents −> db.returnEverythingBetween50And60.insertOne({"Amount":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3c42eedc6604c74817cdb") } > db.returnEverythingBetween50And60.insertOne({"Amount":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3c432edc6604c74817cdc") } > db.returnEverythingBetween50And60.insertOne({"Amount":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3c436edc6604c74817cdd") } > db.returnEverythingBetween50And60.insertOne({"Amount":59}); {    "acknowledged" : true, ... Read More

Can MongoDB return result of increment?

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

98 Views

Yes, you can achieve this with findAndModify(). Let us first create a collection with documents −> db.returnResultOfIncementDemo.insertOne({"PlayerScore":98}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3c292edc6604c74817cda") }Following is the query to display all documents from a collection with the help of find() method −> db.returnResultOfIncementDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd3c292edc6604c74817cda"), "PlayerScore" : 98 }Following is the query to return result of increment. Here, we have incremented PlayerScore by 2 −> db.returnResultOfIncementDemo.findAndModify({ ...   query:{}, ...   update: { $inc: {PlayerScore: 2 }}, ...   new: true ... });This will produce the following output −{ "_id" : ... Read More

Get the size of all the documents in a MongoDB query?

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

198 Views

To get the size of all the documents in a query, you need to loop through documents. Let us first create a collection with documents −> db.sizeOfAllDocumentsDemo.insertOne({"StudentFirstName":"John", "StudentSubject":["MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3c0c1edc6604c74817cd7") } > db.sizeOfAllDocumentsDemo.insertOne({"StudentFirstName":"Larry", "StudentSubject":["MySQL", "PHP"], "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3c0d9edc6604c74817cd8") } > db.sizeOfAllDocumentsDemo.insertOne({"StudentFirstName":"Chris", "StudentSubject":["SQL Server", "C#"], "StudentAge":23, "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd3c0fbedc6604c74817cd9") }Following is the query to display all documents from a collection with the help of find() method −> db.sizeOfAllDocumentsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd3c0c1edc6604c74817cd7"),   ... Read More

How to insert an 8-byte integer into MongoDB through JavaScript shell?

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

180 Views

You can use below syntax for inserting an 8-byte integer in MongoDB through JavaScript shell −anyVariableName= {"yourFieldName" : new NumberLong("yourValue")};To display the above variable, you can use the variable name or printjson(). Following is the query to insert 8 byte integer in MongoDB −> userDetail = {"userId" : new NumberLong("98686869")}; { "userId" : NumberLong(98686869) }Let us display the above variable value using variable name. The query is as follows −> userDetailThis will produce the following output −{ "userId" : NumberLong(98686869) }Let us display the variable value using printjson() −> printjson(userDetail); This will produce the following output −{ "userId" : NumberLong(98686869) }

How to access subdocument value when the key is a number in MongoDB?

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

108 Views

To access subdocument value, let us first create a collection with documents −> db.accessSubDocumentDemo.insertOne( ...    { ... ...       "Details" : { ...          "1" : { ...             "StudentLowerScore" : "33", ...             "StudentHoghScore" : "55" ...          }, ...          "2" : { ...             "StudentLowerScore" : "45", ...             "StudentHoghScore" : "65" ...          }, ...          "3" : ... Read More

Advertisements