MongoDB Articles

Page 86 of 111

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

Count distinct value in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 445 Views

To count distinct values in MongoDB, use the distinct() method combined with the .length property. This returns the number of unique values for a specified field across all documents in a collection. Syntax db.collectionName.distinct("fieldName").length; Create Sample Data Let us create a collection with documents that contain duplicate values ? db.countDistinctDemo.insertMany([ {"StudentName": "John"}, {"StudentName": "Chris"}, {"StudentName": "Chris"}, {"StudentName": "Carol"}, {"StudentName": "David"}, {"StudentName": "Carol"} ]); { ...

Read More

Get index of given element in array field in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 1K+ Views

To get the index of a given element in an array field in MongoDB, use the $indexOfArray operator within an aggregation pipeline. This operator returns the zero-based index of the first occurrence of the specified element in the array. Syntax db.collection.aggregate([ { $project: { "fieldName": { $indexOfArray: [ "$arrayField", "searchElement" ] ...

Read More

How to query MongoDB using the $ne operator?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 200 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

Unset an attribute from a single array element in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 236 Views

To unset an attribute from a single array element in MongoDB, use the $unset operator combined with the $ positional operator to target the specific array element that matches your query condition. Syntax db.collection.update( {"arrayField.attribute": "matchValue"}, {$unset: {"arrayField.$.attribute": 1}} ) Create Sample Data db.unsetAnAttributeDemo.insertOne({ _id: 1, "StudentDetails": [ { "StudentFirstName": "Ramit", ...

Read More

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

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 279 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

MongoDB query to find the highest numeric value of a column?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 248 Views

To find the highest numeric value of a column in MongoDB, use $type operator with $not to filter only numeric values, then apply sort() and limit() to get the maximum value. Syntax db.collection.find({ "field": { $not: { $type: "string" } } }).sort({ "field": -1 }).limit(1); Sample Data db.highestNumericValueOfAColumnDemo.insertMany([ { "StudentName": "John", "StudentMathMarks": 69 }, { ...

Read More

Delete a field and value in MongoDB?

karthikeya Boyini
karthikeya Boyini
Updated on 15-Mar-2026 491 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

How to read a specific key-value pair from a MongoDB collection?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 985 Views

To read a specific key-value pair from a MongoDB collection, use dot notation in the projection parameter of the find() method. This allows you to retrieve only the nested fields you need. Syntax db.collection.find( {}, { "parentField.childField": 1 } ); Sample Data db.readSpecificKeyValueDemo.insertOne({ "_id": 100, "StudentDetails": { "StudentFirstName": "David", "StudentLastName": "Miller", "StudentAge": 23, ...

Read More

How to remove duplicate values inside a list in MongoDB?

Samual Sam
Samual Sam
Updated on 15-Mar-2026 1K+ Views

To remove duplicate values from an array field in MongoDB, use the aggregation framework with the $setUnion operator. This operator creates a set union between the array and an empty array, automatically eliminating duplicates. Syntax db.collection.aggregate([ { $project: { "fieldName": { $setUnion: ["$arrayField", []] } } } ]); Sample Data db.removeDuplicatesDemo.insertOne({ "InstructorName": "Chris", ...

Read More
Showing 851–860 of 1,106 articles
« Prev 1 84 85 86 87 88 111 Next »
Advertisements