Found 1659 Articles for Big Data Analytics

Find Strings greater than a particular length in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

2K+ 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

How to search in array of object in MongoDB?

Anvi Jain
Updated on 10-Sep-2023 07:55:51

30K+ 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

How to copy a collection from one database to another in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

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

How to list all collections from a particular MongoDB database?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

178 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

How to perform descending order sort in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

309 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

How to perform ascending order sort in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

182 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

Get distinct record values in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

365 Views

You can use distinct() method in MongoDB to get distinct record values. The syntax is as follows −db.yourCollectionName.distinct(“yourFieldName”);To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows −> db.distinctRecordDemo.insertOne({"StudentId":1, "StudentName":"John", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c77a78299b97a65744c1b50") } > db.distinctRecordDemo.insertOne({"StudentId":2, "StudentName":"John", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c77a78b99b97a65744c1b51") } > db.distinctRecordDemo.insertOne({"StudentId":3, "StudentName":"Carol", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c77a79a99b97a65744c1b52") } > db.distinctRecordDemo.insertOne({"StudentId":4, "StudentName":"Carol", "StudentAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c77a7a499b97a65744c1b53") } > db.distinctRecordDemo.insertOne({"StudentId":5, "StudentName":"Sam", "StudentAge":24}); ... Read More

MongoDB SELECT COUNT GROUP BY?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

226 Views

You can achieve this with the help of an aggregate framework. 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.countGroupByDemo.insertOne({"StudentId":10, "StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7700871e9c5dd6f1f78296") } > db.countGroupByDemo.insertOne({"StudentId":10, "StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c77008f1e9c5dd6f1f78297") } > db.countGroupByDemo.insertOne({"StudentId":20, "StudentName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7700971e9c5dd6f1f78298") } > db.countGroupByDemo.insertOne({"StudentId":30, "StudentName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7700a21e9c5dd6f1f78299") } > db.countGroupByDemo.insertOne({"StudentId":30, "StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7700aa1e9c5dd6f1f7829a") } ... Read More

Find MongoDB records where array field is not empty?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

2K+ Views

You can use $ne(Not Equal) operator for this. To understand the concept, let us create a collection with document. The query to create a collection with document is as follows −> db.arrayFieldIsNotEmptyDemo.insertOne({"StudentName":"Larry", "StudentTechnicalSubject":["Java", "C"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76fe2f1e9c5dd6f1f78291") } > db.arrayFieldIsNotEmptyDemo.insertOne({"StudentName":"Mike", "StudentTechnicalSubject":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76fe3b1e9c5dd6f1f78292") } > db.arrayFieldIsNotEmptyDemo.insertOne({"StudentName":"Sam", "StudentTechnicalSubject":["MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76fe491e9c5dd6f1f78293") } > db.arrayFieldIsNotEmptyDemo.insertOne({"StudentName":"Carol", "StudentTechnicalSubject":[]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76fe521e9c5dd6f1f78294") } > db.arrayFieldIsNotEmptyDemo.insertOne({"StudentName":"David", "StudentTechnicalSubject":["MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76fe661e9c5dd6f1f78295") }Display all documents ... Read More

Check that Field Exists with MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:25

170 Views

You can use the $exists and $ne operator for this. To understand the concept further, let us create a collection with document. The query to create a collection with document is as follows −> db.checkFieldExistDemo.insertOne({"EmployeeId":1, "EmployeeName":"John", "isMarried":true, "EmployeeSalary":4648585}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c76f7b31e9c5dd6f1f78281") } > db.checkFieldExistDemo.insertOne({"StudentId":2, "StudentName":"John", "isMarried":false, " StudentAge":19}); {    "acknowledged" : true, 0    "insertedId" : ObjectId("5c76f7e11e9c5dd6f1f78282") }Display all documents from a collection with the help of find() method. The query is as follows −> db.checkFieldExistDemo.find().pretty();Output{    "_id" : ObjectId("5c76f7b31e9c5dd6f1f78281"),    "EmployeeId" : 1,    "EmployeeName" : "John",    "isMarried" : true,   ... Read More

Advertisements