Nishtha Thakur

Nishtha Thakur

398 Articles Published

Articles by Nishtha Thakur

Page 7 of 40

Write a C macro PRINT(x) which prints x

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 2K+ Views

Here we will see how to define a macro called PRINT(x), and this will print whatever the value of x, passed as an argument. To solve this problem, we will use the stringize operator (#). Using this operator the x is converted into string, then by calling the printf() function internally, the value of x will be printed. Syntax #define PRINT(x) printf(#x) Example Let us see the example to get the better idea − #include #define PRINT(x) printf(#x) int main() { PRINT(Hello); ...

Read More

Scansets in C

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 4K+ Views

Scansets in C are special format specifiers supported by the scanf() family of functions. They allow you to specify which characters to accept or reject when reading input. Scansets are represented by %[] and provide fine-grained control over input validation. Syntax %[characters] // Accept only specified characters %[^characters] // Accept all characters except specified ones %[character-range] // Accept characters within a range Example 1: Basic Character Set This example demonstrates how to read only uppercase letters using a character ...

Read More

Count number of elements in an array with MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

To count the number of elements in an array in MongoDB, use the $size operator within the aggregation framework. The $size operator returns the number of elements in the specified array field. Syntax db.collection.aggregate([ { $project: { fieldName: { $size: "$arrayField" } } } ]); Sample Data Let us create a sample collection with array data ? ...

Read More

How to query all items in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 366 Views

To query all items in a MongoDB collection, use the find() method. This method retrieves all documents from a collection when called without parameters, or you can specify query criteria and projection options to filter results. Syntax db.collection.find() db.collection.find(query, projection) Create Sample Data Let us first create a collection with documents − db.queryAllItemsDemo.insertMany([ { "StudentDetails": { "StudentName": "John", ...

Read More

How to dynamically build MongoDB query?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 2K+ Views

Dynamic MongoDB queries allow you to build query conditions programmatically based on runtime parameters. This is useful when query criteria depend on user input or application logic. Syntax function dynamicQuery(parameters) { var query = {}; // Build query conditions based on parameters if (condition) { query["field"] = { "$operator": value }; } return query; } Sample Data db.dynamicQueryDemo.insertMany([ { "Name": "John", ...

Read More

Replace an array field value with MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 355 Views

To replace an array field value in MongoDB, use the $ positional operator with $set. The positional operator identifies the array element that matches the query condition and replaces it with the new value. Syntax db.collection.update( { "arrayField": "valueToReplace" }, { $set: { "arrayField.$": "newValue" } } ); Sample Data db.replaceAnArrayFieldValueDemo.insertOne({ "StudentTechnicalSubjects": ["MySQL", "SQL Server", "PL/SQL"] }); { "acknowledged": true, "insertedId": ObjectId("5cea41e0ef71edecf6a1f68f") } View Current Document ...

Read More

Filter by several array elements in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 241 Views

To filter documents by several array elements in MongoDB, use the $elemMatch operator. This operator matches documents containing an array field with at least one element that satisfies all specified query criteria. Syntax db.collection.find({ arrayField: { $elemMatch: { field1: "value1", field2: "value2" } } }); Sample Data ...

Read More

Update a single list item of a MongoDB document?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 233 Views

To update a single list item in MongoDB, use the positional operator ($) to target the first array element that matches your query condition. This operator allows you to update specific fields within array elements without affecting other items in the list. Syntax db.collection.update( { "arrayField.field": "matchValue" }, { $set: { "arrayField.$.field": "newValue" } } ); Sample Data Let us first create a collection with documents ? db.updateASingleListDemo.insertOne({ _id: 1, "EmployeeName": "Chris", ...

Read More

Convert a field to an array using MongoDB update operation?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 1K+ Views

To convert a field to an array in MongoDB, use the $set operator to replace the existing field value with an array containing that value. This is useful when you need to transform single values into arrays for future operations. Syntax db.collection.update( { _id: ObjectId("id") }, { $set: { "fieldName": [existingValue] } } ) Sample Data Let us first create a collection with a document having a string field ? db.convertAFieldToAnArrayDemo.insertOne({ "StudentSubject": "MongoDB" }); { ...

Read More

Find documents where all elements of an array have a specific value in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 15-Mar-2026 198 Views

To find documents where all elements of an array have a specific value in MongoDB, use the $not operator with $elemMatch to exclude documents that contain any element NOT matching the desired value. Syntax db.collection.find({ "arrayField": { $not: { $elemMatch: { "field": { $ne: "specificValue" } ...

Read More
Showing 61–70 of 398 articles
« Prev 1 5 6 7 8 9 40 Next »
Advertisements