Too return a specific field, use the find() method in MongoDB. Let us first create a collection with documents −> db.specificFieldDemo.insertOne({"FirstName":"John", "LastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb8019a623186894665ae31") } > db.specificFieldDemo.insertOne({"FirstName":"John", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb801ab623186894665ae32") } > db.specificFieldDemo.insertOne({"FirstName":"David", "LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb801b3623186894665ae33") } > db.specificFieldDemo.insertOne({"FirstName":"Sam", "LastName":"Williams"}); { "acknowledged" : true, "insertedId" : ObjectId("5cb801bf623186894665ae34") }Following is the query to display all documents from the collection with the help of find() method −> db.specificFieldDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cb8019a623186894665ae31"), ... Read More
Before getting into the example, we should know what Intent service is in android. Intent Service is going to do background operation asynchronously. When user call startService() from activity, it doesn’t create an instance for each request and it going to stop service after done some action in service class or else we need to stop service using stopSelf().This example demonstrates How to refresh Activity when IntentService is finished.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code ... Read More
A Septet class is a Tuple of seven elements. It is part of the JavaTuples library.The following is the declaration −public final class Septet extends Tuple implements IValue1, IValue2, IValue3, IValue4, IValue5, IValue6, IValue7Let us first see what we need to work with JavaTuples. To work with Septet class in JavaTuples, you need to import the following package −import org.javatuples.Septet;Note − Steps to download and run JavaTuples program If you are using Eclipse IDE to run Septet Class in JavaTuples, then Right Click Project → Properties → Java Build Path → Add External Jars and upload the downloaded JavaTuples jar ... Read More
It is the ODBC – JDBC bridge driver, it acts as a bridge between JDBC and, ODBC database connectivity mechanism. Using this you can access the databases which support only ODBC. Initially, it is used extensively, since most of the databases supported only ODBC.Whenever Java application sends a request to the JDBC-ODBC bridge driver the request internally calls the ODBC equivalent function and the ODBC driver retrieves the result from the underlying database and sends it back to the JDBC-ODBC bridge driver.Advantages of type1 driverFollowing are the advantages of a type1 driver.Using this single driver, you can access different DataSource.You ... Read More
This example demonstrate about How to get system data directory information in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken text view to show data directory information.Step 3 − Add the following code to src/MainActivity.javapackage com.example.myapplication; import android.app.KeyguardManager; import android.content.Context; import android.net.ConnectivityManager; import android.net.Network; import android.net.NetworkInfo; import android.os.Build; import android.os.Bundle; import android.os.Environment; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class MainActivity extends ... Read More
The forEachOrdered() method of the DoubleStream class in Java performs an action for each element of this stream. This assures that each element is processed in encounter order for streams that have a defined encounter order.The syntax is as followsvoid forEachOrdered(DoubleConsumer action)Here, DoubleConsumer represents an operation that accepts a single double-valued argument and returns no result. The parameter action is a non-interfering action to perform on the elements.To use the DoubleStream class in Java, import the following packageimport java.util.stream.DoubleStream;The following is an example to implement DoubleStream forEachOrdered() method in Java:Example Live Demoimport java.util.stream.DoubleStream; public class Demo { public static void ... Read More
The iterator() method of AbstractList class is used to return an iterator over the elements in this list in proper sequence.The syntax is as followspublic Iterator iterator()Here, the Iterator is an iterator over a collection. To work with the AbstractList class, import the following packageimport java.util.AbstractList;The following is an example to implement iterator() method of the AbstractlList class in JavaExample Live Demoimport java.util.ArrayList; import java.util.Iterator; import java.util.AbstractList; public class Demo { public static void main(String[] args) { AbstractList myList = new ArrayList(); myList.add(75); myList.add(100); myList.add(150); ... Read More
Here we will see what is dynamic memory allocation in C. The C programming language provides several functions for memory allocation and management. These functions can be found in the header file. The following functions for memory allocations.FunctionDescriptionvoid *calloc(int num, int size);This function allocates an array of num elements each of which size in bytes will be size.void free(void *address);This function releases a block of memory block specified by address.void *malloc(int num);This function allocates an array of num bytes and leave them uninitialized.void *realloc(void *address, int newsize);This function re-allocates memory extending it upto newsize.Allocating memory dynamicallyWhile programming, if you ... Read More
To insert current datetime in MongoDB, use the $setOnInsert operator. Let us first implement the following query to create a collection with documents>db.addCurrentDateTimeDemo.insertOne({"StudentName":"John", "StudentAdmissionDate":new Date("2012-01-21") }); { "acknowledged" : true, "insertedId" : ObjectId("5c97ae45330fd0aa0d2fe49f") } >db.addCurrentDateTimeDemo.insertOne({"StudentName":"Carol", "StudentAdmissionDate":new Date("2013-05-24") }); { "acknowledged" : true, "insertedId" : ObjectId("5c97ae54330fd0aa0d2fe4a0") } >db.addCurrentDateTimeDemo.insertOne({"StudentName":"Carol", "StudentAdmissionDate":new Date("2019-07-26") }); { "acknowledged" : true, "insertedId" : ObjectId("5c97ae5f330fd0aa0d2fe4a1") }Following is the query to display all documents from a collection with the help of find() method> db.addCurrentDateTimeDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c97ae45330fd0aa0d2fe49f"), "StudentName" : "John", "StudentAdmissionDate" : ISODate("2012-01-21T00:00:00Z") } { ... Read More
To get total number of fields in all tables in database, you can use information_schema.columns along with aggregate function count(*).We are using ‘sample’ database which consists of a lot of tables with fields. Following is the query to get total number of fields in all tables in database:mysql> SELECT COUNT(*) AS TOTAL_NUMBER_OF_FIELDS -> FROM INFORMATION_SCHEMA.COLUMNS -> WHERE TABLE_SCHEMA = 'sample';This will produce the following output+------------------------+ | TOTAL_NUMBER_OF_FIELDS | +------------------------+ | 796 | +------------------------+ 1 row in set (0.04 sec)Now, let us check another database ‘test’. Following is the query ... Read More