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 9 of 111
MongoDB compound conditions to fetch documents?
MongoDB compound conditions allow you to fetch documents by combining multiple criteria using logical operators. This enables complex filtering with AND, OR, and other conditional operators. Syntax // AND condition (implicit) db.collection.find({field1: "value1", field2: "value2"}) // OR condition db.collection.find({$or: [{field1: "value1"}, {field2: "value2"}]}) // IN operator for multiple values db.collection.find({field: {$in: ["value1", "value2"]}}) Sample Data db.demo714.insertMany([ {FirstName: "Chris", LastName: "Brown"}, {FirstName: "Adam", LastName: "Smith"}, {FirstName: "David", LastName: "Miller"}, {FirstName: "John", LastName: "Doe"} ]); ...
Read MoreUpdate tag records in MongoDB quickly
To update tag records in MongoDB quickly, use the $ positional operator along with the update command. The $ operator identifies the first array element that matches the query condition and updates it efficiently. Syntax db.collection.update( { "arrayField.field": "matchValue" }, { $set: { "arrayField.$.field": "newValue" } } ); Create Sample Data Let us create a collection with tag documents ? db.demo713.insertOne({ tags: [ { ...
Read MoreSort id and reverse the items with MongoDB
The $natural operator returns documents in natural insertion order. To reverse the items and get them in reverse insertion order, use $natural:-1 with the sort() method. Syntax db.collection.find().sort({$natural: -1}); Sample Data Let us create a collection with documents ? db.demo710.insertMany([ {id: 101, Name: "Robert"}, {id: 102, Name: "Carol"}, {id: 103, Name: "Mike"}, {id: 104, Name: "Sam"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreMongoDB aggregation to get two documents with the least marks
To get the two documents with the least marks in MongoDB, use the aggregation pipeline with $sort to order by marks in ascending order and $limit: 2 to restrict the result to only two documents. Syntax db.collection.aggregate([ { $sort: { "field": 1 } }, { $limit: 2 } ]); Sample Data db.demo709.insertMany([ { "Name": "John", "Marks": 75 }, { "Name": "Chris", "Marks": 45 }, { "Name": "David", "Marks": 54 }, ...
Read MoreFind MongoDB records based on a condition?
To find MongoDB records based on a condition, use the find() method with a query document that specifies the filtering criteria. The query document contains field-value pairs that define the conditions to match. Syntax db.collection.find({ field: value }); db.collection.find({ field: { $operator: value } }); Sample Data Let us create a collection with student documents ? db.students.insertMany([ { "Name": "John", "Marks": 54 }, { "Name": "Chris", "Marks": 35 }, { "Name": "David", "Marks": 45 }, { ...
Read MoreSet server status to inactive in a MongoDB collection with server records?
To set a server status to inactive in a MongoDB collection, use the positional operator $ with the $set update operator to target specific array elements based on matching criteria. Syntax db.collection.update( { "arrayField.matchField": "matchValue" }, { $set: { "arrayField.$.updateField": "newValue" } } ); Sample Data Let us create a collection with server records − db.demo707.insertOne({ id: 101, "serverInformation": [ { ...
Read MoreDisplay MongoDB with document and subdocument example and update
MongoDB allows storing documents with subdocuments using nested arrays and objects. This structure enables complex data relationships within a single document, making it ideal for hierarchical data. Syntax db.collectionName.insertOne({ fieldName: "value", subdocumentArray: [ { nestedField1: "value1", nestedField2: "value2" } ] }); Sample Data ...
Read MoreMongoDB query to match documents with array values greater than a specific value
To match documents with array values greater than a specific value in MongoDB, use the $elemMatch operator. The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria. Syntax db.collection.find( { "arrayField": { $elemMatch: { $gt: value } } } ); Create Sample Data Let us create a collection with documents − db.demo701.insertMany([ { "ListOfValues": [100, 200, 300] }, { "ListOfValues": [500, 700, 1000] }, ...
Read MoreMongoDB query to display documents with a specific name irrespective of case
To display documents with a specific name irrespective of case in MongoDB, use the $regex operator with the i flag for case-insensitive matching. This allows you to find documents containing a specific string value regardless of uppercase or lowercase letters. Syntax db.collection.find({ "field": { $regex: /pattern/i } }); Sample Data db.demo700.insertMany([ { details: [{ Name: "david" }] }, { details: [{ Name: "Chris" }] }, { details: [{ Name: "DAVID" }] }, { ...
Read MoreMultiple atomic updates using MongoDB?
Multiple atomic updates in MongoDB allow you to update multiple documents that match a query condition in a single operation. Use the updateMany() method or the legacy update() method with the multi parameter set to true. Syntax // Modern approach db.collection.updateMany( { filter }, { $set: { field: "newValue" } } ); // Legacy approach db.collection.update( { filter }, { $set: { field: "newValue" } }, { multi: true } ); Sample Data ...
Read More