- 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 1359 Articles for MongoDB
958 Views
The following is the syntax to create MongoDB stored procedure −db.system.js.save ( { _id:"yourStoredProcedueName", value:function(argument1,....N) { statement1, . . N } } );Now implement the above syntax. The query to create a stored procedure is as follows −> db.system.js.save ( { _id:"addTwoValue", value:function(a,b) { return a+b } } );The following is the output −WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "addTwoValue" })Now you can call the stored procedure using eval(). The query is as follows −> db.eval("return addTwoValue(100,25)"); 125
546 Views
You can use positional operator $ for this. 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.incrementValueInNestedArrayDemo.insertOne( ... {"UniqueId":1, ... "StudentDetails": ... [ ... { ... "StudentId":101, ... "StudentMarks":97 ... }, ... { ... "StudentId":103, ... "StudentMarks":99 ... Read More
446 Views
To find value in the array with multiple criteria, for example, you can use $elemMatch along with $gt and $lt. The syntax is as follows −db.yourCollectionName.find({yourFieldName:{$elemMatch:{$gt:yourNegativeValue, $lt:yourPo sitiveValue}}}).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.findValueInArrayWithMultipleCriteriaDemo.insertOne({"StudentName":"Larry", "StudentMarks":[-150, 150]}); { "acknowledged" : true, "insertedId" : ObjectId("5c77daf6fc4e719b197a12f5") } > db.findValueInArrayWithMultipleCriteriaDemo.insertOne({"StudentName":"Mike", "StudentMarks":[19]}); { "acknowledged" : true, "insertedId" : ObjectId("5c77db09fc4e719b197a12f6") }Display all documents from a collection with the help of find() method. The query is as follows −> db.findValueInArrayWithMultipleCriteriaDemo.find().pretty();The following is the ... Read More
2K+ Views
You can use $regex operator to check if a field contains a string in MongoDB. The syntax is as follows −db.yourCollectionName.findOne({"yourFieldName":{$regex:".*yourValue.*"}});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.checkFieldContainsStringDemo.insertOne({"Id":1, "Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d762fc4e719b197a12ed") } > db.checkFieldContainsStringDemo.insertOne({"Id":2, "Name":"Johnson"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d76bfc4e719b197a12ee") } > db.checkFieldContainsStringDemo.insertOne({"Id":3, "Name":"Carol"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d774fc4e719b197a12ef") } > db.checkFieldContainsStringDemo.insertOne({"Id":4, "Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77d77cfc4e719b197a12f0") } > db.checkFieldContainsStringDemo.insertOne({"Id":5, "Name":"Sam"}); ... Read More
1K+ Views
To find the string which has a length greater than a particular value in MongoDB, use the $where operator. The syntax is as follows −db.yourCollectionName.find({$where:'this.yourStringFieldName.length > yourIntegerValue'}).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.stringFieldLengthDemo.insertOne({"UserId":1, "UserName":"Adam Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb4b2386c62d05142a78") } > db.stringFieldLengthDemo.insertOne({"UserId":2, "UserName":"Carol Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb562386c62d05142a79") } > db.stringFieldLengthDemo.insertOne({"UserId":3, "UserName":"James Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77bb5b2386c62d05142a7a") } > db.stringFieldLengthDemo.insertOne({"UserId":4, "UserName":"John Smith"}); { "acknowledged" ... Read More
29K+ Views
To search the array of object in MongoDB, you can use $elemMatch operator. This operator allows us to search for more than one component from an array object.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.searchArrayDemo.insertOne({"EmployeeFirstName":"Adam", "EmployeeLastName":"Smith", "EmployeeDateOfBirth":new ISODate("1992-01-31 13:45:10"), ... "EmployeeSkills":["Spring and Hibernate Framework", "Machine Learning"], ... "EmployeeDetails":[ ... { ... "EmployeePerformanceArea":"Java", ... "Year":2001 ... }, ... { ... ... Read More
2K+ Views
In MongoDB, the command does not exist to copy a collection from one database to another. To achieve it, use the below concept −db.yourCollectionName.find().forEach(function(yourVariableName){ db.getSiblingDB('yourDestinationDatabase')['yourCollectionName'].insert(yourVariableName); });Let us create a collection in the test database and copy this collection to another database with the name „sample‟.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 −> use test switched to db test > db.copyThisCollectionToSampleDatabaseDemo.insertOne({"User_Id":101, "UserName":"Larr y"}); { "acknowledged" : true, "insertedId" : ObjectId("5c77ad622386c62d05142a67") } > db.copyThisCollectionToSampleDatabaseDemo.insertOne({"User_Id":102, "UserName":"Maxwell"}); { "acknowledged" : true, ... Read More
166 Views
If you want to list all collections from a particular database then you need to switch the database first. The query is as follows −> use sample; switched to db sample > db.getCollectionNames();The following is the output −[ "copyThisCollectionToSampleDatabaseDemo", "deleteDocuments", "deleteDocumentsDemo", "deleteInformation", "employee", "internalArraySizeDemo", "sourceCollection", "updateInformation", "userInformation" ]An alternate query can be the following −> show collections;The following is the output −copyThisCollectionToSampleDatabaseDemo deleteDocuments deleteDocumentsDemo deleteInformation employee internalArraySizeDemo sourceCollection updateInformation userInformation
285 Views
To sort in ascending order, the syntax is as follows −db.yourCollectionName.find().sort({yourField:1});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.sortingDemo.insertOne({"Value":100}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e2ed3c9d04998abf006") } > db.sortingDemo.insertOne({"Value":1}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e31d3c9d04998abf007") } > db.sortingDemo.insertOne({"Value":150}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e34d3c9d04998abf008") } > db.sortingDemo.insertOne({"Value":250}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e37d3c9d04998abf009") } > db.sortingDemo.insertOne({"Value":5}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e3bd3c9d04998abf00a") } > db.sortingDemo.insertOne({"Value":199}); { "acknowledged" : ... Read More
172 Views
To sort in ascending order, the syntax is as follows −db.yourCollectionName.find().sort({yourField:1});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.sortingDemo.insertOne({"Value":100}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e2ed3c9d04998abf006") } > db.sortingDemo.insertOne({"Value":1}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e31d3c9d04998abf007") } > db.sortingDemo.insertOne({"Value":150}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e34d3c9d04998abf008") } > db.sortingDemo.insertOne({"Value":250}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e37d3c9d04998abf009") } > db.sortingDemo.insertOne({"Value":5}); { "acknowledged" : true, "insertedId" : ObjectId("5c8f8e3bd3c9d04998abf00a") } > db.sortingDemo.insertOne({"Value":199}); { "acknowledged" : ... Read More
To Continue Learning Please Login
Login with Google