karthikeya Boyini

karthikeya Boyini

1,420 Articles Published

Articles by karthikeya Boyini

Page 59 of 142

How can I update and increment two fields in one command in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 363 Views

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 More

Create array with MongoDB query?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 3K+ Views

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 More

How to query MongoDB using the $ne operator?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 208 Views

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 More

Search by property name for any document with that property in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 282 Views

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 More

Delete a field and value in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 507 Views

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 More

Fetch records in MongoDB on querying its subset

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 188 Views

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 More

MongoDB query to get specific month|year (not date)?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 776 Views

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 More

Finding matching records with LIKE in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 198 Views

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 More

Difference between find() and findOne() methods in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 5K+ Views

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 More

How to apply a condition only if field exists in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 2K+ Views

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
Showing 581–590 of 1,420 articles
« Prev 1 57 58 59 60 61 142 Next »
Advertisements