
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 1349 Articles for MongoDB

323 Views
You can use the concept of map reduce to get the position in an array. Let us first create a collection with documents −> db.retrievePositionDemo.find(); { "_id" : ObjectId("5cd569ec7924bb85b3f4893f"), "Subjects" : [ "MySQL", "MongoDB", "Java" ] }Following is the query to display all documents from a collection with the help of find() method −> db.retrievePositionDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cd569ec7924bb85b3f4893f"), "Subjects" : [ "MySQL", "MongoDB", "Java" ] }Following is the query to retrieve the position in an array in MongoDB ... Read More

650 Views
For MongoDB multidimensional array projection, you need to use aggregate framework. Let us first create a collection with documents. Here, we have multidimensional array for Student marks −> db.multiDimensionalArrayProjection.insertOne( ... { ... "StudentFirstName" : "Chris", ... "StudentMarks" : [ [98, 99], [56, 79] ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc6b75a9cb58ca2b005e66c") }Following is the query to display all documents from a collection with the help of find() method −> db.multiDimensionalArrayProjection.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cc6b75a9cb58ca2b005e66c"), "StudentFirstName" : "Chris", "StudentMarks" ... Read More

136 Views
You can use aggregate framework. Let us first create a collection with documents −> db.topCountArrayDemo.insertOne( ... {"StudentId":101 , "StudentSubject": ["C", "MongoDB"]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc6b3209cb58ca2b005e669") } > db.topCountArrayDemo.insertOne( ... {"StudentId":102 , "StudentSubject": ["C", "Java"]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc6b3219cb58ca2b005e66a") } > db.topCountArrayDemo.insertOne( ... {"StudentId":103 , "StudentSubject": ["C", "MongoDB"]} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc6b3229cb58ca2b005e66b") }Following is the query to display all documents from a collection with the help of find() method −> db.topCountArrayDemo.find().pretty();This will produce the following output −{ ... Read More

194 Views
To create a user in MongoDB v3, use the createUser() method. This allows you to create a user and while creating you need to add user, password, and roles as well. These roles assign permissions. The syntax is as follows −use admin db.createUser( { user: “yourUserName", pwd: "yourPassword", roles: [ { role: "yourPermission", db: "yourDatabase" } ] } );Let us implement the above syntax in order to create a user In MongoDB v3 −> use admin switched to db admin > db.createUser( ... { ... ... Read More

412 Views
To select two fields and return a sorted array with distinct values, use aggregate framework along with $setUnion operator. Let us first create a collection with documents −> db.sortedArrayWithDistinctDemo.insertOne( ... { value1: 4, value2: 5} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc690b99cb58ca2b005e666") } > db.sortedArrayWithDistinctDemo.insertOne( ... {value1: 5, value2: 6} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc690b99cb58ca2b005e667") } > db.sortedArrayWithDistinctDemo.insertOne( ... {value1: 7, value2: 4} ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc690b99cb58ca2b005e668") }Following is the query to display all documents from a collection with the ... Read More

343 Views
You can use $type operator to check whether a filed contains null value. Let us first create a collection with documents. We have also inserted a null in a field −> db.nullDemo.insertOne({"FirstName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5cc68a1eac184d684e3fa270") } > db.nullDemo.insertOne({"FirstName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5cc68a25ac184d684e3fa271") } > db.nullDemo.insertOne({"FirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cc68a2cac184d684e3fa272") }Following is the query to display all documents from a collection with the help of find() method −> db.nullDemo.find().pretty();This will produce the following output. One of the field is null −{ "_id" : ObjectId("5cc68a1eac184d684e3fa270"), "FirstName" ... Read More

545 Views
Let us first create a collection with a document −>db.useResultDemo.insertOne({"StudentFirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5cda70f5b50a6c6dd317adcd") }Display all documents from a collection with the help of find() method −> db.useResultDemo.find();Following is the output −{ "_id" : ObjectId("5cda70f5b50a6c6dd317adcd"), "StudentFirstName" : "Robert" }Here is the query to use result from MongoDB in shell script with var keyword −> var studentName=db.useResultDemo.findOne({},{_id:0}); > studentNameFollowing is the output −{ "StudentFirstName" : "Robert" }

331 Views
Let us first create a collection with documents −> db.arrayContainOnlySpecificFieldDemo.insertOne( ... { ... "StudentName":"John", ... "StudentAge":21, ... "StudentTechnicalSubject":["C", "Java", "MongoDB"] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5cc4921dac184d684e3fa26a") } > db.arrayContainOnlySpecificFieldDemo.insertOne( { "StudentName":"Carol", "StudentAge":23, "StudentTechnicalSubject":["MongoDB"] } ); { "acknowledged" : true, "insertedId" : ObjectId("5cc49237ac184d684e3fa26b") }Display all documents from a collection with the help of find() method. The query is as follows −> db.arrayContainOnlySpecificFieldDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cc4921dac184d684e3fa26a"), "StudentName" : "John", "StudentAge" : 21, ... Read More

1K+ Views
To get attribute list from MongoDB object, you can use for loop to extract key and value for document. Let us create a collection with documents −>db.getAttributeListDemo.insertOne({"StudentId":101, "StudentName":"John", "StudentAdmissi onDate":new ISODate('2019-01-12'), "StudentSUbjects":["MongoDB", "Java", "MySQL"]}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdfcc9ac184d684e3fa269") }Display all documents from a collection with the help of find() method −> db.getAttributeListDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbdfcc9ac184d684e3fa269"), "StudentId" : 101, "StudentName" : "John", "StudentAdmissionDate" : ISODate("2019-01-12T00:00:00Z"), "StudentSUbjects" : [ "MongoDB", "Java", "MySQL" ] }Following is the ... Read More

143 Views
You can use findOne() for this. Following is the syntax −db.yourCollectionName.findOne({yourFieldName: 'yourValue'});Let us create a collection with documents −> db.checkExistingDemo.insertOne({"StudentName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdf90dac184d684e3fa265") } > db.checkExistingDemo.insertOne({"StudentName":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdf912ac184d684e3fa266") } > db.checkExistingDemo.insertOne({"StudentName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdf916ac184d684e3fa267") } > db.checkExistingDemo.insertOne({"StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdf91bac184d684e3fa268") }Display all documents from a collection with the help of find() method −> db.checkExistingDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbdf90dac184d684e3fa265"), "StudentName" : "John" } { "_id" : ObjectId("5cbdf912ac184d684e3fa266"), "StudentName" : "Carol" } ... Read More