Found 1349 Articles for MongoDB

How do you remove an array element by its index in MongoDB

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

1K+ Views

To remove array element by its index in MongoDB, you can use $unset and $pull operator. There are two steps to remove array elements from an array.The syntax for the same is as follows:db.yourCollectionName.update({}, {$unset:{"yourArrayListName.yourPosition":yourPositionValue}}; db.yourCollectionName.update({}, {$pull:{"yourArrayListName":null}});To understand the above syntax, let us create a collection with document. The query to create a collection with document is as follows:>db.removeArrayElements.insertOne({"StudentName":"Larry", "StudentAge":23, "TechnicalSub ject":["C", "C++", "Java", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ea4879c4643706aef56d2") }Display all documents from a collection with the help of find() method. The query is as follows:> db.removeArrayElements.find().pretty();The following is the output:{    "_id" : ObjectId("5c6ea4879c4643706aef56d2"), ... Read More

Best way to store date/time in MongoDB?

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

1K+ Views

There are two different ways by which you can store date/time in MongoDB. In the first approach, you can use Date objects like JavaScript. The Date object is the best way to store date/time in MongoDB. The syntax is as follows:new Date();In the second approach, you can use ISODate(). The syntax is as follows:new ISODate();To understand the above syntax, let us create a collection with documents following the first approach. The query to create a collection with document is as follows:The first approach:> db.ProductsInformation.insertOne({"ProductId":"Product-1", "ProductDeliveryDateTime":new Date()}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c6ec6786fd07954a4890686") }The second approach:> db.ProductsInformation.insertOne({"ProductId":"Product-2", "ProductDeliveryDateTime":new ... Read More

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

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

704 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

704 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

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

Advertisements