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 karthikeya Boyini
Page 59 of 142
How can I update and increment two fields in one command in MongoDB?
To update and increment two fields in one command in MongoDB, use the $inc operator with multiple field-value pairs in a single update operation. This allows you to increment multiple numeric fields atomically in one database call. Syntax db.collection.update( { query }, { $inc: { field1: value1, field2: value2 } } ); Create Sample Data Let us first create a collection with documents ? db.incrementDemo.insertOne({ "Value1": 10, "Value2": 20 }); { ...
Read MoreCreate array with MongoDB query?
You can use the toArray() method with MongoDB queries to convert query results into an array format. This method transforms the cursor returned by find() into a JavaScript array containing all matching documents. Syntax db.collectionName.find({}, {fieldName: 1}).toArray(); Sample Data Let us create a collection with sample documents ? db.createArrayDemo.insertMany([ {"UserName": "Chris"}, {"UserName": "David"}, {"UserName": "Robert"}, {"UserName": "Sam"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreHow to query MongoDB using the $ne operator?
To query MongoDB using the $ne operator, use it to find documents where a field is not equal to a specified value. The $ne operator excludes documents that match the given value. Syntax db.collection.find({fieldName: {$ne: value}}); Create Sample Data Let us create a collection with student documents ? db.notEqualToDemo.insertMany([ {"StudentName": "Larry", "StudentMathMarks": 68}, {"StudentName": "Chris", "StudentMathMarks": 88}, {"StudentName": "David", "StudentMathMarks": 45}, {"StudentName": "Carol", "StudentMathMarks": 69} ]); { "acknowledged": true, ...
Read MoreSearch by property name for any document with that property in MongoDB?
To search for documents that contain a specific property in MongoDB, use the $ne operator with a null value. This will return all documents where the property exists and is not null. Syntax db.collection.find({ propertyName: { $ne: null } }) Sample Data db.searchByPropertyName.insertMany([ { "FirstName": "Larry", "Age": 23 }, { "FirstName": null, "Age": 21 }, { "FirstName": "John", "Age": 22 }, { "FirstName": null, "Age": 25 }, { "FirstName": "David", "Age": 20 } ...
Read MoreDelete a field and value in MongoDB?
To delete a field and its value from MongoDB documents, use the $unset operator. This operator removes the specified field from all matched documents in the collection. Syntax db.collection.update( { field: { $exists: true } }, { $unset: { fieldName: 1 } }, { multi: true } ) Sample Data Let us first create a collection with sample documents − db.deleteFieldDemo.insertMany([ { "FirstName": "John", "LastName": "Smith" }, { "FirstName": "David", "LastName": "Miller" ...
Read MoreFetch records in MongoDB on querying its subset
To fetch records in MongoDB that contain a subset of values within an array field, use the $all operator. This operator matches documents where the array field contains all the specified elements, regardless of their order or position. Syntax db.collection.find({ "arrayField": { $all: ["value1", "value2", "value3"] } }); Sample Data db.subsetOfAnArrayDemo.insertOne({ "StudentProgrammingSkills": [ "Java", "MongoDB", "MySQL", "C++", "Data Structure", "Algorithm", "Python", "Oracle", "SQL Server" ...
Read MoreMongoDB query to get specific month|year (not date)?
To get specific month or year values from MongoDB date fields, use the aggregation framework with $month and $year operators. These operators extract the numeric month (1-12) and year values from date fields. Syntax db.collection.aggregate([ { $project: { field: 1, dateField: { $month: "$dateField" } } }, { $match: { dateField: monthNumber } } ]); Sample Data db.specificMonthDemo.insertMany([ { "StudentName": "Larry", "StudentDateOfBirth": new ISODate("1995-01-12") }, { "StudentName": "Chris", "StudentDateOfBirth": new ISODate("1999-12-31") }, { ...
Read MoreFinding matching records with LIKE in MongoDB?
To perform LIKE operations in MongoDB, use regular expressions (regex) with the find() method. MongoDB doesn't have a LIKE operator like SQL, but regex patterns provide equivalent functionality for pattern matching in text fields. Syntax db.collection.find({ "field": /pattern/flags }) Common patterns: /^text/ - Starts with "text" /text$/ - Ends with "text" /text/ - Contains "text" /text/i - Case-insensitive matching Sample Data db.likeDemo.insertMany([ {"Name": "John", "Age": 32}, {"Name": "Chris", "Age": 25}, {"Name": "Carol", "Age": 22}, ...
Read MoreDifference between find() and findOne() methods in MongoDB?
The findOne() method returns the first document if a query matches, otherwise returns null. The find() method never returns null — it always returns a cursor object, even when no documents match. Syntax // findOne() - returns single document or null db.collection.findOne(query, projection); // find() - returns cursor object db.collection.find(query, projection); Sample Data Let us create an empty collection to demonstrate the behavior ? db.createCollection('emptyCollection'); { "ok" : 1 } Check the document count in the collection ? db.emptyCollection.count(); 0 ...
Read MoreHow to apply a condition only if field exists in MongoDB?
To apply a condition only if a field exists in MongoDB, use the $or operator combined with $exists. This allows you to match documents where a field either doesn't exist or meets a specific condition. Syntax db.collection.find({ $or: [ { fieldName: { $exists: false } }, { fieldName: condition } ] }); Sample Data Let's create a collection with some documents where the StudentAge field is missing in one document ...
Read More