Firstly, set a a new ArrayList and add elements to it.ArrayList arr = new ArrayList(); arr.Add( "Jones" ); arr.Add( "Tom" ); arr.Add( "Henry" );Now let’s remove the item “Tom”. For that, use the Remove() method.arr.Remove("Tom");The following is the complete example to remove an element from ArrayList −Example Live Demousing System; using System.Collections; class Demo { static void Main(){ ArrayList arr = new ArrayList(); arr.Add( "Jones" ); arr.Add( "Tom" ); arr.Add( "Henry" ); Console.WriteLine("Initial ArrayList..."); foreach(string str in arr) { ... Read More
Take the help of $addToSet in MongoDB to specify a return format. Let us create a collection with documents −> db.demo207.insertOne({"FavouriteTechnology":"Spring Boot"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8e7a03d395bdc21346f1") } > db.demo207.insertOne({"FavouriteTechnology":"MongoDB"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8e8f03d395bdc21346f2") } > db.demo207.insertOne({"FavouriteTechnology":"Groovy"}); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8ea603d395bdc21346f3") }Display all documents from a collection with the help of find() method −> db.demo207.find();This will produce the following output −{ "_id" : ObjectId("5e3d8e7a03d395bdc21346f1"), "FavouriteTechnology" : "Spring Boot" } { "_id" : ObjectId("5e3d8e8f03d395bdc21346f2"), "FavouriteTechnology" : "MongoDB" } { "_id" : ObjectId("5e3d8ea603d395bdc21346f3"), "FavouriteTechnology" : "Groovy" }Following is ... Read More
To determine if a specific value does not exist, use $ne in MongoDB. Let us create a collection with documents −> db.demo206.insertOne( ... { ... "ClientDetails": ... [ ... { ... "Name":"Chris", ... "Age":28, ... "CountryName":"US" ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8bd403d395bdc21346ee") } > db.demo206.insertOne( ... { ... "ClientDetails": ... [ ... ... Read More
To count records on the basis of matching criteria, use count(). Let us create a collection with documents −> db.demo205.insertOne( ... { ... ... "id": "101", ... "Name": "", ... "Age": "", ... "isActive": false ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8a3003d395bdc21346eb") } > db.demo205.insertOne( ... { ... ... "id": "102", ... "Name": "Chris", ... "Age": "25", ... "isActive": true ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3d8a3003d395bdc21346ec") } > db.demo205.insertOne( ... Read More
To display only an element found in an array, use aggregate(). Let us create a collection with documents −> db.demo204.insertOne( ... { ... "_id" : 101, ... "Name" : "Chris", ... "Age" : 23, ... "details" : [ ... { ... "id" : "1001", ... "empId" : "John_1001", ... "salary" : "50000", ... "Technology" : "Java" ... }, ... ... Read More
To query nested array, use $elemMatch in MongoDB. Let us create a collection with documents −> db.demo203.insertOne({ ... "_id" : "101", ... "Name" : "Chris", ... "details1" : [ ... { ... "empName" : "David", ... "salary" : "50000", ... "technology" : [ ... "MySQL", ... "MongoDB" ... ] ... }, ... { ... "empName" : "Carol", ... ... Read More
For information on query plan, use explain() in MongoDB. Let us create a collection with documents −> db.demo202.insertOne({"StudentFirstName":"Chris", "StudentAge":21}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c3bd103d395bdc21346e8") } > db.demo202.insertOne({"StudentFirstName":"David", "StudentAge":23}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c3bd803d395bdc21346e9") } > db.demo202.insertOne({"StudentFirstName":"Bob", "StudentAge":22}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c3bde03d395bdc21346ea") }Display all documents from a collection with the help of find() method −> db.demo202.find();This will produce the following output −{ "_id" : ObjectId("5e3c3bd103d395bdc21346e8"), "StudentFirstName" : "Chris", "StudentAge" : 21 } { "_id" : ObjectId("5e3c3bd803d395bdc21346e9"), "StudentFirstName" : "David", "StudentAge" : 23 } { "_id" : ObjectId("5e3c3bde03d395bdc21346ea"), "StudentFirstName" ... Read More
To update only a specific value, use update() and in that set the new value using $set. Let us create a collection with documents −> db.demo201.insertOne({"ClientName":"Chris Brown", "ClientAge":26}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c39ca03d395bdc21346e5") } > db.demo201.insertOne({"ClientName":"David Miller", "ClientAge":35}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c39d403d395bdc21346e6") } > db.demo201.insertOne({"ClientName":"Carol Taylor", "ClientAge":28}); { "acknowledged" : true, "insertedId" : ObjectId("5e3c39e603d395bdc21346e7") }Display all documents from a collection with the help of find() method −> db.demo201.find();This will produce the following output −{ "_id" : ObjectId("5e3c39ca03d395bdc21346e5"), "ClientName" : "Chris Brown", "ClientAge" : 26 } { "_id" : ObjectId("5e3c39d403d395bdc21346e6"), ... Read More
To filter multiple sub-documents in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo200.insertOne( ... { ... "Id":"101", ... "details1":[ ... { ... "isActive":true, ... "SubjectName":"MySQL" ... }, { ... "isActive":false, ... "SubjectName":"Java" ... } ... ], ... "details2":[ ... { ... "isActive":false, ... ... Read More
For aggregation in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo199.insertOne( ... { ... "details1":{ ... "details2":{ ... "details3":{ ... "Name":"Chris", ... "details4":{ ... "details5":{ ... "v1":10, ... "v2":20, ... "v3":30 ... ... Read More