Found 1669 Articles for Big Data Analytics

How to remove an element from a doubly-nested array in a MongoDB document?

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

2K+ Views

To remove an element from a doubly-nested array in MongoDB document, you can use $pull operator.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.removeElementFromDoublyNestedArrayDemo.insertOne(    ... {       ... "_id" : "1",       ... "UserName" : "Larry",       ... "UserDetails" : [          ... {             ... "UserCountryName" : "US",             ... "UserLocation" : [                ... { ... Read More

Include all existing fields and add new fields to document in MongoDB?

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

324 Views

You can achieve this with the help of $addFields operator. 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.addFieldDemo.insertOne({"EmployeeId":101, "EmployeeName":"Larry", "EmployeeDetails":{    "EmployeeSalary":65000, "EmployeeCity":"New York", "Message":"Hi"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f654d8d10a061296a3c44") }Display all documents from a collection with the help of find() method. The query is as follows −> db.addFieldDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c7f654d8d10a061296a3c44"),    "EmployeeId" : 101,    "EmployeeName" : "Larry",    "EmployeeDetails" : {       "EmployeeSalary" : 65000,     ... Read More

Auto increment in MongoDB to store sequence of Unique User ID?

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

532 Views

To auto increment in MongoDB to store a sequence of unique user id, let us create a collection which contains information about last sequence values of all documents.Let us first create a collection. The query to create a collection which is as follows −> db.createSequenceDemo.insertOne({_id:"SID", S_Value:0}); { "acknowledged" : true, "insertedId" : "SID" }Now, we will create a function that will generate an auto increment in MongoDB to store sequence. The query is as follows −> function nextSequence(s) {    ... var sd = db.createSequenceDemo.findAndModify({       ... query:{_id: s },       ... update: {$inc:{S_Value:1}},     ... Read More

Find all duplicate documents in a MongoDB collection by a key field?

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

281 Views

Use the aggregate framework to find all duplicate documents in a MongoDB collection by a key field.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.findDuplicateByKeyDemo.insertOne({"StudentId":1, "StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f5b168d10a061296a3c3a") } > db.findDuplicateByKeyDemo.insertOne({"StudentId":2, "StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f5b1f8d10a061296a3c3b") } > db.findDuplicateByKeyDemo.insertOne({"StudentId":3, "StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f5b248d10a061296a3c3c") } > db.findDuplicateByKeyDemo.insertOne({"StudentId":4, "StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f5b2d8d10a061296a3c3d") } > db.findDuplicateByKeyDemo.insertOne({"StudentId":5, "StudentName":"Sam"}); {    "acknowledged" : true, ... Read More

How to convert string to numerical values in MongoDB?

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

378 Views

You need to use some code in order to convert a string to numerical values in MongoDB.Let us first create a collection with a document. The query to create a collection with a document is as follows:> db.convertStringToNumberDemo.insertOne({"EmployeeId":"101", "EmployeeName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f56528d10a061296a3c31") } > db.convertStringToNumberDemo.insertOne({"EmployeeId":"1120", "EmployeeName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f56648d10a061296a3c32") } > db.convertStringToNumberDemo.insertOne({"EmployeeId":"3210", "EmployeeName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7f566e8d10a061296a3c33") }Display all documents from the collection with the help of find() method. The query is as follows −> db.convertStringToNumberDemo.find().pretty();The following is the output −{    "_id" ... Read More

What is the $unwind operator in MongoDB?

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

259 Views

The $unwind operator in MongoDB is the same for each array, it returns the mapping document. Here is the demo of $unwind operator in MongoDB.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.unwindOperatorDemo.insertOne({"StudentName":"Larry", "StudentAge":23, "StudentSubje ct":["C", "C++", "Java", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ef5f3559dd2396bcfbfc8") }Display all documents from a collection with the help of find() method. The query is as follows −> db.unwindOperatorDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c7ef5f3559dd2396bcfbfc8"),    "StudentName" : "Larry",    "StudentAge" : ... Read More

MongoDB find by multiple array items?

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

778 Views

You can use $all operator to find by multiple array items. 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.findByMultipleArrayDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith",    "StudentCoreSubject":["Compiler", "Operating System", "Computer Networks"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ef07b559dd2396bcfbfc4") } > db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylor",    "StudentCoreSubject":["MongoDB", "MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ef09d559dd2396bcfbfc5") } > db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"Bob", "StudentLastName":"Taylor",    "StudentCoreSubject":["MongoDB", "MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ef0c7559dd2396bcfbfc6") } > db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Johnson",    "StudentCoreSubject":["Compiler", "Operating System", "Computer Networks"]}); {   ... Read More

MongoDB find by multiple array items using $in?

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

99 Views

You can use $in operator to find with multiple array items. 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.findByMultipleArrayDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith",    "StudentCoreSubject":["Compiler", "Operating System", "Computer Networks"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ef07b559dd2396bcfbfc4") } >db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylor",    "StudentCoreSubject":["MongoDB", "MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ef09d559dd2396bcfbfc5") } >db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"Bob", "StudentLastName":"Taylor",    "StudentCoreSubject":["MongoDB", "MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ef0c7559dd2396bcfbfc6") } >db.findByMultipleArrayDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Johnson",    "StudentCoreSubject":["Compiler", "Operating System", "Computer Networks"]}); {    "acknowledged" : true,   ... Read More

Insert records in MongoDB collection if it does not exist?

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

2K+ Views

You can use update() function to insert records in MongoDB if it does not exist. 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.insertIfNotExistsDemo.insertOne({"StudentName":"Mike", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7eec7b559dd2396bcfbfc2") } > db.insertIfNotExistsDemo.insertOne({"StudentName":"Sam", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7eec97559dd2396bcfbfc3") }Display all documents from a collection with the help of find() method. The query is as follows −> db.insertIfNotExistsDemo.find().pretty(); The following is the output: {    "_id" : ObjectId("5c7eec7b559dd2396bcfbfc2"),    "StudentName" : "Mike",    "StudentAge" : ... Read More

How can I rename a field for all documents in MongoDB?

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

239 Views

The syntax is as follows to rename a field for all documents. Here, we have used $renameLdb.yourCollectionName.update({}, {$rename:{"yourOldFieldName":"yourNewFieldName"}}, false, true);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.renameFieldDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ee6c7559dd2396bcfbfbb") } > db.renameFieldDemo.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ee6cb559dd2396bcfbfbc") } > db.renameFieldDemo.insertOne({"StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ee6cf559dd2396bcfbfbd") } > db.renameFieldDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c7ee6d3559dd2396bcfbfbe") } > db.renameFieldDemo.insertOne({"StudentName":"Maxwell"}); {    "acknowledged" : true,    "insertedId" ... Read More

Advertisements