Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles by Nishtha Thakur
Page 7 of 40
Write a C macro PRINT(x) which prints x
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 MoreScansets in C
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 MoreCount number of elements in an array with MongoDB?
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 MoreHow to query all items in MongoDB?
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 MoreHow to dynamically build MongoDB query?
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 MoreReplace an array field value with MongoDB?
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 MoreFilter by several array elements in MongoDB?
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 MoreUpdate a single list item of a MongoDB document?
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 MoreConvert a field to an array using MongoDB update operation?
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 MoreFind documents where all elements of an array have a specific value in MongoDB?
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