
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 1661 Articles for Big Data Analytics

77 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

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

1K+ Views
To retrieve a value from MongoDB by its key name, use the following syntax −db.yourCollectionName.find({}, {"yourFieldName":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.retrieveValueFromAKeyDemo.insertOne({"CustomerName":"Larry", "CustomerAge":21, "CustomerCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9163b5a56efcc0f9e69048") } > db.retrieveValueFromAKeyDemo.insertOne({"CustomerName":"Chris", "CustomerAge":24, "CustomerCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9163c4a56efcc0f9e69049") } > db.retrieveValueFromAKeyDemo.insertOne({"CustomerName":"Mike", "CustomerAge":26, "CustomerCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9163d3a56efcc0f9e6904a") }Display all documents from a collection with the help of find() method. The query is as follows −> db.retrieveValueFromAKeyDemo.find().pretty();The ... Read More

504 Views
In order to delete the collection which has some special characters like _ or -, you need to use the following syntax −db.getCollection("yourCollectionName").drop();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.createCollection("_personalInformation"); { "ok" : 1 } > db.getCollection('_personalInformation').insertOne({"ClientName":"Chris", "ClientCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9158bb4afe5c1d2279d6b2") } > db.getCollection('_personalInformation').insertOne({"ClientName":"Mike", "ClientCountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9158c84afe5c1d2279d6b3") } > db.getCollection('_personalInformation').insertOne({"ClientName":"David", "ClientCountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9158d54afe5c1d2279d6b4") }Display all documents from a collection with the ... Read More

636 Views
To remove the _id element, you can use the following syntax −db.yourCollectionName.find({}, {'_id': false}).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.removingidElementDemo.insertOne({"UserName":"John", ... "UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c9153fd4afe5c1d2279d6ad") } > db.removingidElementDemo.insertOne({"UserName":"Carol", "UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c9154084afe5c1d2279d6ae") } > db.removingidElementDemo.insertOne({"UserName":"David", "UserAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c9154154afe5c1d2279d6af") } > db.removingidElementDemo.insertOne({"UserName":"Mike", "UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c9154204afe5c1d2279d6b0") } > db.removingidElementDemo.insertOne({"UserName":"Chris", "UserAge":20}); { "acknowledged" : true, ... Read More

524 Views
There is no in-built function to count a number of keys in a document. In order to count a number of keys, you need to write some code.Let us create a collection with a document. The query to create a collection with a document is as follows −> db.numberofKeysInADocumentDemo.insertOne({ "UserName":"John", "UserAge":21, "UserEmailId":"john12@gmail.com", "UserCountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5c9132584afe5c1d2279d6ac") }Display all documents from a collection with the help of find() method. The query is as follows −> db.numberofKeysInADocumentDemo.find().pretty();The following is the output −{ "_id" : ObjectId("5c9132584afe5c1d2279d6ac"), "UserName" : "John", "UserAge" : 21, ... Read More

340 Views
You can use “$regex” operator to implement the equivalent of SQL ‘like’ in MongoDB. To implement it, let us create a collection with a document. The query to create a collection with a document is as follows −> db.sqlLikeDemo.insertOne({"UserName":"John Smith", "UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e124afe5c1d2279d6a5") } > db.sqlLikeDemo.insertOne({"UserName":"John Doe", "UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e264afe5c1d2279d6a6") } > db.sqlLikeDemo.insertOne({"UserName":"Chris Williams", "UserAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e404afe5c1d2279d6a7") } > db.sqlLikeDemo.insertOne({"UserName":"Robert Taylor", "UserAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5c912e4d4afe5c1d2279d6a8") } > db.sqlLikeDemo.insertOne({"UserName":"John Brown", "UserAge":27}); { "acknowledged" ... Read More

680 Views
PyMongo is a Python distribution containing tools for working with MongoDB. Use the following syntax to use a variable for collection name −var yourVariableName="yourCollectionName"; db[storeCollectionName].yourOperationName;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. We have used variable for collection name −> var storeCollectionName="new_Collection"; > db[storeCollectionName].insertOne({"UserName":"John", "UserAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5c912aea4afe5c1d2279d6a0") } > db[storeCollectionName].insertOne({"UserName":"Carol", "UserAge":24}); { "acknowledged" : true, "insertedId" : ObjectId("5c912af54afe5c1d2279d6a1") } > db[storeCollectionName].insertOne({"UserName":"Mike", "UserAge":27}); { "acknowledged" : true, "insertedId" : ObjectId("5c912afe4afe5c1d2279d6a2") }Display ... Read More

926 Views
To get the database data size in MongoDB, you can use stats() method. The syntax is as follows −db.stats();Let us use the database with the name ‘test’.Now, check the current database with the help of the following query −> db;The following is the output −testHere is the query to get database data size in MongoDB −> db.stats()The following is the output −{ "db" : "test", "collections" : 114, "views" : 0, "objects" : 391, "avgObjSize" : 83.0076726342711, "dataSize" : 32456, "storageSize" : 3211264, "numExtents" : 0, "indexes" : 120, "indexSize" ... Read More

322 Views
To find a document with Objectid in MongoDB, use the following syntax −db.yourCollectionName.find({"_id":ObjectId("yourObjectIdValue")}).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.findDocumentWithObjectIdDemo.insertOne({"UserName":"Larry"}); { "acknowledged" : true, "insertedId" : ObjectId("5c90e4384afe5c1d2279d69b") } > db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Mike", "UserAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5c90e4444afe5c1d2279d69c") } > db.findDocumentWithObjectIdDemo.insertOne({"UserName":"Carol", "UserAge":26, "UserHobby":["Learning", "Photography"]}); { "acknowledged" : true, "insertedId" : ObjectId("5c90e4704afe5c1d2279d69d") }Display all documents from a collection with the help of find() method. The query is as follows −> db.findDocumentWithObjectIdDemo.find().pretty();The following is the output ... Read More