Found 1719 Articles for Big Data Analytics

How to update the _id of a MongoDB Document?

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

10K+ Views

You cannot update it but you can save a new id and remove the old id. Follow some steps in order to update the _id of a MongoDB. The steps are as follows:Step1: In the first step, you need to store ObjectId into a variable.anyVariableName=db.yourCollectionName.findOne({_id:yourObjectIdValue)});Step 2: In the second step, you need to set a new id.yourDeclaredVariableName._id=yourNewObjectIdValue;Step 3: In the third step, you need to insert new id on a document.db.yourCollectionName.insert(yourDeclaredVariableName);Step 4: In the fourth step, you need to remove the old id.db.yourCollectionName.remove({_id:yourOldObjectIdValue)});To understand the above steps, let us create a collection with document. The query to create a collection ... Read More

Count the number of items in an array in MongoDB?

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

5K+ Views

To count the number of items in an array, you can use $size operator. The syntax is as follows:db.yourCollectionName.aggregate({$project:{anyFieldName:{$size:"$yourArrayName"}}}).prett y();To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:>db.getSizeOfArray.insertOne({"StudentId":1, "StudentName":"Larry", "StudentMarks":[87, 34, 5 6, 77, 89, 90]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ebc536fd07954a4890680") } >db.getSizeOfArray.insertOne({"StudentId":2, "StudentName":"Sam", "StudentMarks":[90, 76, 56 ]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ebc6b6fd07954a4890681") } >db.getSizeOfArray.insertOne({"StudentId":3, "StudentName":"Carol", "StudentMarks":[90, 76]}) ; {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ebc7a6fd07954a4890682") }Now you can display all documents from a collection with ... Read More

How to select a single field in MongoDB?

Daniol Thomas
Updated on 14-Sep-2023 15:35:43

25K+ Views

You can select a single field in MongoDB using the following syntax:db.yourCollectionName.find({"yourFieldName":yourValue}, {"yourSingleFieldName":1, _id:0});In the above syntax "yourSingleFieldName":1, _id:0 means get all data from one field without _id.To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:> db.singleFieldDemo.insertOne({"StudentName":"David", "StudentAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6eba356fd07954a489067c") } > db.singleFieldDemo.insertOne({"StudentName":"Bob", "StudentAge":18}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6eba406fd07954a489067d") } > db.singleFieldDemo.insertOne({"StudentName":"Chris", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6eba4c6fd07954a489067e") } > db.singleFieldDemo.insertOne({"StudentName":"Robert", "StudentAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6eba586fd07954a489067f") ... Read More

Remove object from array in MongoDB?

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

1K+ Views

To remove object from an array in MongoDB, you can use $pull operator. The syntax is as follows:db.yourCollectionName.update( {'_id':ObjectId("5c6ea036a0c51185aefbd14f")}, {$pull:{"yourArrayName":{"yourArrayFieldName":yourValue}}}, false, true);To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:> db.removeObject.insertOne({"CustomerName":"Maxwell", "CustomerAge":23, ... "CustomerDetails":[ ... { ... "CustomerId":100, ... "CustomerProduct":"Product-1" ... }, ... { ... "CustomerId":150, ... "CustomerProduct":"Product-2" ... }, ... { ... "CustomerId":200, ... "CustomerProduct":"Product-3" ... } ... ] ... }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ea036a0c51185aefbd14f") }Display all documents from a collection with the help of find() method. The query is ... Read More

How to remove array element in MongoDB?

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

668 Views

To remove array element in MongoDB, you can use $pull and $in operator. The syntax is as follows:db.yourCollectionName.update({},    {$pull:{yourFirstArrayName:{$in:["yourValue"]}, yourSecondArrayName:"yourValue"}},    {multi:true} );To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:>db.removeArrayElement.insertOne({"StudentName":"Larry", "StudentCoreSubject":["MongoD B", "MySQL", "SQL Server", "Java"], "StudentFavouriteTeacher":["John", "Marry", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ec9c46fd07954a4890688") }Display all documents from a collection with the help of find() method. The query is as follows:> db.removeArrayElement.find().pretty();The following is the output:{    "_id" : ObjectId("5c6ec9c46fd07954a4890688"),    "StudentName" : "Larry",    "StudentCoreSubject" : [     ... Read More

Find document with array that contains a specific value in MongoDB

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

651 Views

You can use find() method to find document with array that contains a specific value. The syntax is as follows:db.yourCollectionName.find({"yourArrayFieldName":"yourValue"}, .......N).pretty();To understand the above syntax, let us create a collection with documents. The query to create a collection with documents is as follows:>db.findSpecificValue.insertOne({"StudentId":1, "StudentName":"Larry", "FavouriteSubject":["C", "C++", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6e8996140577d89182b8d0") } >db.findSpecificValue.insertOne({"StudentId":2, "StudentName":"Larry", "FavouriteSubject":["MongoDB", "MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6e89b1140577d89182b8d1") }Display all documents from a collection with the help of find() method. The query is as follows:> db.findSpecificValue.find().pretty();The following is the output:{    "_id" : ObjectId("5c6e8996140577d89182b8d0"),    "StudentId" ... Read More

Update field in exact element array in MongoDB?

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

195 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

289 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

834 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

549 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

Advertisements