Programming Articles

Page 1080 of 2547

Connecting MongoDB with NodeJS

Mayank Agarwal
Mayank Agarwal
Updated on 15-Mar-2026 2K+ Views

To connect MongoDB with Node.js, use the MongoClient.connect() method from the mongodb package. This asynchronous method establishes a connection between your Node.js application and the MongoDB server. Syntax MongoClient.connect(url, options, callback) Parameters: url − Connection string specifying the MongoDB server location and port options − Optional configuration object (e.g., useUnifiedTopology: true) callback − Function executed after connection attempt with error and client parameters Installation and Setup First, install the MongoDB driver for Node.js ? npm install mongodb --save Start your MongoDB server ? mongod --dbpath=data ...

Read More

How to prepare a Python date object to be inserted into MongoDB?

SaiKrishna Tavva
SaiKrishna Tavva
Updated on 14-Mar-2026 2K+ Views

MongoDB stores dates in ISODate format. PyMongo (the official MongoDB driver for Python) directly supports Python's datetime.datetime objects, which are automatically converted to ISODate when inserted. There are three common ways to prepare date objects for MongoDB. Method 1: Current UTC Timestamp Use datetime.datetime.utcnow() to insert the current UTC time ? import datetime from pymongo import MongoClient client = MongoClient() db = client.test_database result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Date Object inserted") Date Object inserted Method 2: Specific Date Use datetime.datetime(year, month, day, hour, minute) for a fixed date ? ...

Read More

How do you convert an ArrayList to an array in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 769 Views

An ArrayList provides two toArray() methods to convert it into an array − one returns an Object[] array, and the other returns a typed array. Method 1: toArray() (Returns Object[]) Object[] toArray() Returns an array containing all elements in proper sequence. The returned array type is Object[], so casting is needed to use specific types. Method 2: toArray(T[]) (Returns Typed Array) T[] toArray(T[] a) Returns a typed array containing all elements. Pass an empty array of the desired type and Java allocates the correct size. This is the preferred ...

Read More

How do I add an element to an array list in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 959 Views

We can add elements to an ArrayList easily using its add() method. The method appends the specified element to the end of the list, or inserts it at a specific index. Syntax boolean add(E e) void add(int index, E element) The first form appends the element to the end. The second form inserts the element at the specified index, shifting existing elements to the right. Example The following example shows how to add elements to an ArrayList using both forms of the add() method ? import java.util.ArrayList; import java.util.List; public ...

Read More

Difference between ArrayBlockingQueue and LinkedBlockingQueue

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 585 Views

ArrayBlockingQueue and LinkedBlockingQueue both implement the BlockingQueue interface from the java.util.concurrent package. Both store elements in FIFO order, are thread-safe, and do not accept null elements. They differ in their internal data structure, capacity behavior, and locking mechanism. ArrayBlockingQueue ArrayBlockingQueue is backed by a fixed-size array. Once created, the capacity cannot be changed. It uses a single lock with two conditions (notEmpty and notFull) for both put and take operations, meaning producers and consumers cannot operate concurrently. LinkedBlockingQueue LinkedBlockingQueue is backed by linked nodes. It is optionally bounded − if no capacity is specified, it defaults ...

Read More

Difference between grep and fgrep command

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 895 Views

Both grep and fgrep are Linux commands used to search for strings in files, directories, or command output. The key difference is that grep supports regular expressions, while fgrep treats the search pattern as a fixed (literal) string. grep (Global Regular Expression Print) grep searches for strings or regular expressions in files. It interprets special characters like ., *, ^, $, [ ] as regex metacharacters. It uses the Boyer-Moore algorithm for fast searching. fgrep (Fixed grep / grep -F) fgrep (equivalent to grep -F) searches for fixed strings only. It does not recognize regular expressions ...

Read More

Difference between strncmp() and strcmp() in C/C++

George John
George John
Updated on 14-Mar-2026 2K+ Views

Both strncmp() and strcmp() are used in C/C++ programs for lexicographical string comparison. The strcmp() compares two strings till the null character is found, whereas strncmp() only compares a specified number of characters. What is strncmp() ? The function strncmp() is used to compare left string to right string up to a number. It works same as strcmp(). It returns a value greater than zero when the matching character of left string has greater ASCII value than the character of the right string. Returns a value less than zero when the matching character of left string has lesser ASCII value ...

Read More

Difference between the Ternary operator and Null coalescing operator in php

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 564 Views

In PHP, the ternary operator (?:) and the null coalescing operator (??) are both shorthand for conditional expressions. The ternary operator evaluates a condition and returns one of two values, while the null coalescing operator specifically checks if a variable is set and not null. Ternary Operator (?:) The ternary operator replaces if-else statements into a single expression − Syntax: (condition) ? expression1 : expression2; Equivalent: if (condition) { return expression1; } else { return expression2; } If the condition is true, it returns expression1; ...

Read More

How do you create a list in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 8K+ Views

A Java List can be created in multiple ways depending on whether you need a modifiable list, a fixed-size list, or want to initialize it with values in a single statement. Way 1: Raw Type (Not Recommended) Create a List without specifying the type of elements. The compiler will show an unchecked warning − List list = new ArrayList(); Way 2: Generic Type (Recommended) Create a List with a specified element type for type safety − List list = new ArrayList(); Way 3: Initialize in One Line Create ...

Read More

How do you add two lists in Java?

Mahesh Parahar
Mahesh Parahar
Updated on 14-Mar-2026 1K+ Views

The addAll() method of the List interface can be used to combine two lists in Java. It comes in two variants − one appends elements at the end, and another inserts elements at a specific index. addAll() Without Index Appends all elements from the specified collection to the end of the list − boolean addAll(Collection

Read More
Showing 10791–10800 of 25,466 articles
Advertisements