- 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 1659 Articles for Big Data Analytics
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
923 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
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
87 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
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
190 Views
Yes, Mongo shell treats numbers as float by default. To work it as int or any other type, you need to mention explicitly. You can use NumberInt() for this. The syntax is as follows −var anyVariableName= [NumberInt("yourValue1"), NumberInt("yourValue2"), .....N];Let us implement the above syntax in order to treat numbers as integer only (not float) −> var integerArrayDemo = [NumberInt("50"), NumberInt("60"), NumberInt("70"), NumberInt("90"), NumberInt("40")];Following is the query to display the array value −> printjson(integerArrayDemo);This will produce the following output −[ NumberInt(50), NumberInt(60), NumberInt(70), NumberInt(90), NumberInt(40) ]To display the array value, you can use print() −> ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
1K+ Views
In order to convert NumberLong to String in MongoDB, you can use toString() −NumberLong('yourValue').valueOf().toString();To convert NumberLong to String, you can use valueOf() as well −NumberLong('yourValue').valueOf();Let us implement both the above syntaxes.Following is the query to convert NumberLong to String with toString() −> NumberLong('1000').valueOf().toString();This will produce the following output −1000Following is the query to convert NumberLong to Number with valueOf()> NumberLong('1000').valueOf();This will produce the following output −1000
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
699 Views
You can use printjson() or print() to get output of MongoDB shell script. Let us create an array of objects.Following is the query to create an array of objects.> var studentDetails=[{"StudentName":"John","StudentAge":21}, {"StudentName":"Carol","StudentAge":24},{"StudentName":"David","StudentAge":25}];Following is the query to get the output of Mongo shell script using printjson() −> printjson(studentDetails);This will produce the following output −[ { "StudentName" : "John", "StudentAge" : 21 }, { "StudentName" : "Carol", "StudentAge" : 24 }, { "StudentName" : "David", "StudentAge" : 25 } ] > var studentDetails=[{"StudentName":"John","StudentAge":21}, {"StudentName":"Carol","StudentAge":24},{"StudentName":"David","StudentAge":25}];
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
44 Views
You need to switch the database to admin. Following is the syntax −use adminThe syntax is as follows for shutdown option −db.shutdownServer()Let us implement the above syntax for shutdown −> use admin switched to db admin > db.shutdownServer()This will produce the following output −server should be down... 2019-04-22T19:11:40.949+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed 2019-04-22T19:11:42.197+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
165 Views
You can use every() in MongoDB for this. Let us create a collection with documents −> db.arrayConditionDemo.insertOne({"Name":"John", "Marks":[40, 43, 45]}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdbd06de8cc557214c0e1a") } > db.arrayConditionDemo.insertOne({"Name":"Mike", "Marks":[45]}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdbd17de8cc557214c0e1b") } > db.arrayConditionDemo.insertOne({"Name":"Chris", "Marks":[43, 45, 59, 69, 78, 89]}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdbd3cde8cc557214c0e1c") }Display all documents from a collection with the help of find() method. The query is as follows −> db.arrayConditionDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbdbd06de8cc557214c0e1a"), "Name" : "John", "Marks" : [ ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
246 Views
You can use createUser() method for this. Following is the syntax −db.createUser( { user: "yourUserName", pwd: "yourPassword", roles: [ { role: "read", db: "yourDatabaseName" } ] } );Let us create a user in MongoDB. Here, we are using database ‘test’ −> db.createUser( ... { ... user: "David", ... pwd: "David123456", ... roles: [ { role: "read", db: "test" } ] ... } ... );This will produce the following output −Successfully added user: { "user" : "David", "roles" : [ { "role" : "read", "db" : "test" } ] }
![Samual Sam](https://www.tutorialspoint.com/assets/profiles/13514/profile/60_83486-1512649303.jpg)
421 Views
Let us create a collection with documents −> db.selectOnlyNumericDemo.insertOne({"UserId":"User101"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdb711de8cc557214c0e16") } > db.selectOnlyNumericDemo.insertOne({"UserId":"102"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdb716de8cc557214c0e17") } > db.selectOnlyNumericDemo.insertOne({"UserId":"User103"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdb71dde8cc557214c0e18") } > db.selectOnlyNumericDemo.insertOne({"UserId":"104"}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdb725de8cc557214c0e19") }Display all documents from a collection with the help of find() method −> db.selectOnlyNumericDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbdb711de8cc557214c0e16"), "UserId" : "User101" } { "_id" : ObjectId("5cbdb716de8cc557214c0e17"), "UserId" : "102" } { "_id" : ObjectId("5cbdb71dde8cc557214c0e18"), "UserId" : "User103" } { "_id" : ... Read More
![karthikeya Boyini](https://www.tutorialspoint.com/assets/profiles/13518/profile/60_31598-1537784993.jpg)
244 Views
Let us first create a collection with documents −> db.incrementDemo.insertOne({"Value1":10, "Value2":20}); { "acknowledged" : true, "insertedId" : ObjectId("5cbdaf07de8cc557214c0e15") }Display all documents from a collection with the help of find() method. The query is as follows −> db.incrementDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbdaf07de8cc557214c0e15"), "Value1" : 10, "Value2" : 20 }Following is the query to increment two fields in one command in MongoDB −> db.incrementDemo.update({}, { $inc : { Value1 : 1, Value2 : 1 } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Let us check both the fields ... Read More