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
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?
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 MoreLimit number of values in a field using MongoDB?
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 MoreDisplay the last two values from field with MongoDB
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 MoreMongoDB query to get last inserted document?
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 MoreMongoDB query to find data from an array inside an object?
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 MoreProjection result as an array of selected items in MongoDB?
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 MoreRemoving an array element from MongoDB collection using update() and $pull
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 MoreWrite an equality in MongoDB without using $eq operator
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 MoreMongoDB query to find a value from JSON like data?
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 MoreMongoDB Limit fields and slice projection together?
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