MongoDB Articles

Page 69 of 111

MongoDB query to select one field if the other is null and the first field if both are not null?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 491 Views

In MongoDB, to select one field when another is null and the first field when both are not null, use the $ifNull operator in an aggregation pipeline. This operator returns the first non-null value from the provided expressions. Syntax db.collection.aggregate([ { $project: { "fieldName": { "$ifNull": [ "$primaryField", "$fallbackField" ] } } } ]); Sample Data Let us ...

Read More

Limit number of values in a field using MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 409 Views

To limit the number of values in a field using MongoDB, use the $slice projection operator. This operator allows you to return only a specified number of elements from an array field. Syntax db.collection.find( {}, { "arrayField": { "$slice": numberOfElements } } ); Sample Data Let us first create a collection with documents ? db.numberOfValuesDemo.insertOne({ "Values": [100, 200, 300, 900, 1000, 98] }); { "acknowledged": true, "insertedId": ObjectId("5cefb736ef71edecf6a1f6ab") } ...

Read More

Display the last two values from field with MongoDB

George John
George John
Updated on 15-Mar-2026 142 Views

To display the last two values from a field in MongoDB, use the $slice operator with a negative value in the projection. The negative value indicates retrieving elements from the end of the array. Syntax db.collection.find({}, { "fieldName": { "$slice": -N } }); Sample Data Let us first create a collection with documents ? db.numberOfValuesDemo.insertOne({ "Values": [100, 200, 300, 900, 1000, 98] }); { "acknowledged": true, "insertedId": ObjectId("5cefb736ef71edecf6a1f6ab") } Display all documents from a collection ...

Read More

MongoDB query to get last inserted document?

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 14K+ Views

To get the last inserted document in MongoDB, use sort() with _id in descending order along with limit(1). Since ObjectId contains a timestamp, sorting by _id in descending order retrieves the most recently inserted document. Syntax db.collection.find().sort({_id: -1}).limit(1); Sample Data Let us first create a collection with documents ? db.getLastInsertedDocument.insertMany([ {"Name": "John"}, {"Name": "Chris"}, {"Name": "Robert"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

MongoDB query to find data from an array inside an object?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 578 Views

To find data from an array inside an object in MongoDB, use dot notation to navigate through the nested object structure and target the specific array field. Syntax db.collection.find({"objectField.arrayField": "searchValue"}); Sample Data Let us first create a collection with documents ? db.findDataDemo.insertMany([ { "CustomerName": "John", "CustomerDetails": { "CountryName": ["AUS"], ...

Read More

Projection result as an array of selected items in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 201 Views

Use the distinct() method to project results as an array of selected items in MongoDB. This method finds distinct values for a specified field across a collection and returns them in an array format. Syntax db.collection.distinct("field", query) Sample Data Let us first create a collection with documents ? db.projectionListDemo.insertMany([ {"_id": "1", "Subject": ["MongoDB", "MySQL", "Java"]}, {"_id": "2", "Subject": ["MongoDB", "C", "C++"]}, {"_id": "3", "Subject": ["Java", "Python"]} ]); { "acknowledged": true, ...

Read More

Removing an array element from MongoDB collection using update() and $pull

George John
George John
Updated on 15-Mar-2026 243 Views

To remove a specific element from an array in MongoDB, use the $pull operator with the update() method. This operator removes all instances of a value that match the specified condition from an array field. Syntax db.collection.update( { query }, { $pull: { "arrayField": "valueToRemove" } } ); Create Sample Data Let us first create a collection with documents ? db.removingAnArrayElementDemo.insertOne({ "UserMessage": ["Hi", "Hello", "Bye"] }); { "acknowledged": true, ...

Read More

Write an equality in MongoDB without using $eq operator

Chandu yadav
Chandu yadav
Updated on 15-Mar-2026 148 Views

In MongoDB, you can write equality queries without using the $eq operator by using the implicit equality syntax. This involves directly specifying the field name and value in the query document. Syntax db.collection.find({"fieldName": "value"}); Sample Data Let us first create a collection with documents ? db.operatorDemo.insertMany([ {"StudentSubject": ["MongoDB", "MySQL", "Java"]}, {"StudentSubject": ["Java", "C", "C++"]} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5cef94eaef71edecf6a1f6a2"), ...

Read More

MongoDB query to find a value from JSON like data?

Smita Kapse
Smita Kapse
Updated on 15-Mar-2026 5K+ Views

To find a value from JSON-like data in MongoDB, use the find() method along with dot notation to access nested fields within embedded documents and arrays. Syntax db.collection.find({"field.nestedField": "value"}) Sample Data db.findValueFromJsonDemo.insertOne({ "UserDetails": [{ "_id": ObjectId(), "UserName": "Carol", "UserMessage": "Hi" }], "UserFriendsName": ["John", "Sam"] }); { "acknowledged": true, ...

Read More

MongoDB Limit fields and slice projection together?

Anvi Jain
Anvi Jain
Updated on 15-Mar-2026 304 Views

To limit fields and use slice projection together in MongoDB, use the $slice operator within the projection parameter of the find() method. This allows you to control which fields are returned while simultaneously limiting array elements using slice. Syntax db.collection.find( { query }, { "field1": 0, "field2": 0, "arrayField": { "$slice": [startIndex, count] } } ); Sample Data ...

Read More
Showing 681–690 of 1,106 articles
« Prev 1 67 68 69 70 71 111 Next »
Advertisements