
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

243 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

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

833 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}];

76 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

208 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

309 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" } ] }

550 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

300 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

594 Views
You can use $lt operator for this. Let us create a collection with documents −> db.beforeSpecifyDateDemo.insertOne({"UserLoginDate":new ISODate('2016-03-21')}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd91e4de8cc557214c0e0d") } > db.beforeSpecifyDateDemo.insertOne({"UserLoginDate":new ISODate('2016-05-11')}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd91ecde8cc557214c0e0e") } > db.beforeSpecifyDateDemo.insertOne({"UserLoginDate":new ISODate('2017-01-31')}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd91f9de8cc557214c0e0f") } > db.beforeSpecifyDateDemo.insertOne({"UserLoginDate":new ISODate('2018-05-15')}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd9206de8cc557214c0e10") } > db.beforeSpecifyDateDemo.insertOne({"UserLoginDate":new ISODate('2019-04-01')}); { "acknowledged" : true, "insertedId" : ObjectId("5cbd9211de8cc557214c0e11") }Display all documents from a collection with the help of find() method. The query is as follows −> db.beforeSpecifyDateDemo.find().pretty();This will produce the ... Read More

252 Views
To remove item from array, you can use $pull operator. Let us create a collection with documents −> db.removeItemFromArray.insertOne( { "_id":101, "StudentName":"Larry", "StudentSubjects":["C", "MongoDB", "Java", "MySQL"] } ); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method. The query is as follows −> db.removeItemFromArray.find().pretty();This will produce the following output −{ "_id" : 101, "StudentName" : "Larry", "StudentSubjects" : [ "C", "MongoDB", "Java", "MySQL" ] }Following is the query to remove item from an ... Read More