- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 1349 Articles for MongoDB
180 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
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
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
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
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
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
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
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
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
428 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