Found 1659 Articles for Big Data Analytics

Update field in exact element array in MongoDB?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

211 Views

You can update the in exact element array in MongoDB with the help of below statement. The syntax is as follows:{"yourArrayDocumentName.$.yourNestedArrayDocument.yourPosition":"yourValue"}});To understand the above syntax, let us create a collection with some documents. The query to create a collection with document is as follows:> db.updateExactField.insertOne({"ActorId":1, "ActorDetails":[{"ActorName":"Johnny Depp", "MovieList": ["The Tourist", "Public Enemy"]}, ... {"ActorName":"Chris Evans", "MovieList":["Captain America", "Avengers"]}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6d7f63f2db199c1278e7f1") }Now you can display documents from a collection with the help of find() method. The query is as follows:> db.updateExactField.find().pretty();The following is the output:{    "_id" : ObjectId("5c6d7f63f2db199c1278e7f1"),    "ActorId" : 1,   ... Read More

Is it possible to make a case-insensitive query in MongoDB?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

316 Views

Yes, you can use regexp to make a case-insensitive query in MongoDB. The syntax is as follows:db.yourCollectionName.find({"yourFieldName":/^yourvalue$/i});To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows:> db.caseInsensitiveDemo.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6d7a67f2db199c1278e7ef") } > db.caseInsensitiveDemo.insertOne({"Name":"JOHN"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6d7ad6f2db199c1278e7f0") }Display all documents from a collection with the help of find(). The query is as follows:> db.caseInsensitiveDemo.find();The following is the output:{ "_id" : ObjectId("5c6d7a67f2db199c1278e7ef"), "Name" : "John" } { "_id" : ObjectId("5c6d7ad6f2db199c1278e7f0"), "Name" : "JOHN" }Here is the ... Read More

MongoDB $push in nested array?

Krantik Chavan
Updated on 26-Jun-2020 08:36:51

876 Views

Here, $push can be used to add new documents in nested array. To understand the above $push concept, let us create a collection with nested array document. The query to create a collection with document is as follows:>db.nestedArrayDemo.insertOne({"EmployeeName":"Larry", "EmployeeSalary":9000, "EmployeeDetails":    [{"EmployeeDOB":new Date('1990-01-21'), "EmployeeDepartment":"ComputerScience", "EmployeeProject":    [{"Technology":"C", "Duration":6}, {"Technology":"Java", "Duration":7}]}]});The following is the output:{    "acknowledged" : true,    "insertedId" : ObjectId("5c6d73090c3d5054b766a76e") }Now you can display documents from a collection with the help of find() method. The query is as follows:> db.nestedArrayDemo.find().pretty();The following is the output:{    "_id" : ObjectId("5c6d73090c3d5054b766a76e"),    "EmployeeName" : "Larry",    "EmployeeSalary" : 9000,    "EmployeeDetails" ... Read More

Query for documents where array size is greater than 1 in MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25

593 Views

You can use length to query for documents where array size is greater than 1:db.yourCollectionName.find({$where:"this.yourArrayDocumentName.length > 1"}).pretty();To understand the above syntax, let us create a collection with some documents. The query is as follows to create a collection with documents:>db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Larry", "StudentTechnicalSubje ct":["Java", "C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6d6c4c0c3d5054b766a76a") } >db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Maxwell", "StudentTechnicalSu bject":["MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6d6c660c3d5054b766a76b") } >db.arrayLengthGreaterThanOne.insertOne({"StudentName":"Maxwell", "StudentTechnicalSu bject":["MySQL", "SQL Server", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6d6c800c3d5054b766a76c") }Display all documents from a collection with the help of find() method. The query is as follows:> db.arrayLengthGreaterThanOne.find().pretty();The ... Read More

Update objects in a MongoDB documents array (nested updating)?

Nancy Den
Updated on 30-Jul-2019 22:30:25

627 Views

To update the objects in a document’s array, you need to use update() method. To understand the update() method, let us create a collection with document. The query to create a collection with document is as follows:> db.updateObjects.insertOne({"CustomerId":1, "CustomerName":"Larry", "TotalItems":100, ... "ItemDetails":[ ... { ... "NameOfItem":"Item_1", ... "Amount":450 ... }, ... { ... "NameOfItem":"Item_2", ... "Amount":500 ... }, ... { ... "NameOfItem":"Item_3", ... "Amount":200 ... } ... ] ... } ... );The following is the output:{    "acknowledged" : true,    "insertedId" : ObjectId("5c6d688b0c3d5054b766a769") }Now you can display documents from a collection with the help of find() method. The query ... Read More

How to filter array in subdocument with MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25

324 Views

You can use aggregate and unwind the array list before applying match. To understand the above concept, let us create a collection with documents. The query to create a collection with document is as follows:> db.filterArray.insertOne( { "L": [{ "N":1 }, { "N":2 } , { "N":3 }, { "N":4 }, { "N":5 } ]});The following is visible after running the above query:{    "acknowledged" : true,    "insertedId" : ObjectId("5c6d63f2734e98fc0a434aeb") }Display document from a collection with the help of find() method. The query is as follows:> db.filterArray.find().pretty();The following is the output:{    "_id" : ObjectId("5c6d63f2734e98fc0a434aeb"),    "L" : [ ... Read More

Get Random record from MongoDB?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

1K+ Views

To get a random record from MongoDB, you can use aggregate function. The syntax is as follows:db.yourCollectionName.aggregate([{$sample:{size:1}}]);To understand the above syntax, let us create a collection with some documents. The query to create collection is as follows:>db.employeeInformation.insert({"EmployeeId":1, "EmployeeName":"Maxwell", "EmployeeAge":26}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":2, "EmployeeName":"David", "EmployeeAge":25}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":3, "EmployeeName":"Carol", "EmployeeAge":24}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":4, "EmployeeName":"Bob", "EmployeeAge":28}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":5, "EmployeeName":"Sam", "EmployeeAge":27); WriteResult({ "nInserted" : 1 })Now you can display all documents from a collection with the help of find() method. The query is as follows:> db.employeeInformation.find().pretty();The following is the output:{ ... Read More

MongoDB query condition to compare two fields?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

4K+ Views

You can use $where operator along with find() method to compare two fields in MongoDB. The syntax is as follows:db.yourCollectionName.find({$where:”yourCondition”}).pretty();To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows:>db.compareTwoFields.insert({"Id":1, "FullName":{"FirstName":"John", "LastName":"Smith"}, "BranchName":"ComputerScience"}); WriteResult({ "nInserted" : 1 }) >db.compareTwoFields.insert({"Id":2, "FullName":{"FirstName":"Smith", "LastName":"Smith"}, "BranchName":"Civil"}); WriteResult({ "nInserted" : 1 }) >db.compareTwoFields.insert({"Id":3, "FullName":{"FirstName":"David", "LastName":"Smith"}, "BranchName":"Mechanical"}); WriteResult({ "nInserted" : 1 })Now you can display all documents from a collection with the help of find() method. The query is as follows:> db.compareTwoFields.find().pretty();The following is the output:{    "_id" : ObjectId("5c6c237568174aae23f5ef64"),   ... Read More

Get names of all keys in the MongoDB collection?

Nancy Den
Updated on 30-Jul-2019 22:30:25

1K+ Views

The syntax to get names of all keys in the collection is as follows:var yourVariableName1=db.yourCollectionName.findOne(); for(var yourVariableName 2 in yourVariableName1) { print(yourVariableName); }To understand the above syntax, let us create a collection with documents. The collection name we are creating is “studentGetKeysDemo”.The following is the query to create documents:>db.studentGetKeysDemo.insert({"StudentId":1, "StudentName":"Larry", "StudentAge":23, "StudentAddress":"US", ... "StudentHobby":["Cricket", "Football", "ReadingNovel"],    "StudentMathMarks":89, "StudentDOB":ISODate('1998-04-06')});The following is the output:WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method. The query is as follows:> db.studentGetKeysDemo.find().pretty();The following is the output:{    "_id" : ObjectId("5c6c12dd68174aae23f5ef5f"),    "StudentId" : 1,    "StudentName" : ... Read More

How to query MongoDB with “like”?

Nancy Den
Updated on 30-Jul-2019 22:30:25

2K+ Views

You can easily query MongoDB with “like”:db.yourCollectionName.find({"yourFieldName" : /.*yourMatchingValue.*/}).pretty();To understand the above syntax, let us create a collection with some documents. Here, we have a collection with the name ‘employee’. The query is as follows:> db.employee.insert({"EmployeeName":"John", "EmployeeSalary":450000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Carol", "EmployeeSalary":150000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"James", "EmployeeSalary":550000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Jace", "EmployeeSalary":670000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Larry", "EmployeeSalary":1000000}); WriteResult({ "nInserted" : 1 })Now you can display all documents from a collection using find() method. The query is as follows:> db.employee.find().pretty();The following is the output:{    "_id" : ObjectId("5c6c0b2e68174aae23f5ef59"),   ... Read More

Advertisements