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
Articles by AmitDiwan
Page 597 of 840
MongoDB inverse of query to return all items except specific documents?
To get documents except some specific documents, use $nor along with $and. The $nor operator returns documents that do not match any of the specified conditions, making it perfect for inverse queries. Syntax db.collection.find({ $nor: [ { $and: [{ field1: value1 }, { field2: value2 }] } ] }); Sample Data Let us first create a collection with sample student documents — db.demo1.insertMany([ { "StudentName": "Chris", "StudentMarks": 38 }, ...
Read MoreHow to specify the order in which the query returns matching documents in MongoDB
To specify the order in which the query returns matching documents, use cursor.sort() in MongoDB. The cursor is obtained from db.collectionName.find(). Syntax db.collection.find().sort({ field: 1 }); // Ascending order db.collection.find().sort({ field: -1 }); // Descending order Where 1 means ascending order and -1 means descending order. Sample Data Let us create a collection with documents − db.demo259.insertMany([ { "Subject": "MySQL" }, { "Subject": "Java" }, { "Subject": "MongoDB" } ]); { ...
Read MoreHow to pull value from array of ObjectIDs in MongoDB?
To pull value from array of ObjectIDs in MongoDB, use the $pull operator with the $in operator to match and remove specific ObjectID values from the array. Syntax db.collection.update( { /* query */ }, { $pull: { arrayField: { $in: [ObjectId("id1"), ObjectId("id2")] } } } ); Sample Data Let us create a collection with documents ? db.demo258.insertOne({ "arrayOfObjectsId": [ ObjectId("5e47a5e81627c0c63e7dba92"), ObjectId("5e47a5e51627c0c63e7dba91") ...
Read MoreSet MongoDB compound index with a fixed value field
A MongoDB compound index with a fixed value field ensures uniqueness across multiple fields, including fields that may be missing (null) in some documents. When you create a unique compound index, MongoDB treats missing fields as null values. Syntax db.collection.createIndex( { "field1": 1, "field2": 1 }, { unique: true } ); Create Sample Data First, let's create a unique compound index on StudentName and StudentAge ? db.compoundIndexDemo.createIndex( {"StudentName": 1, "StudentAge": 1}, {unique: true} ); ...
Read MoreUpdate MongoDB variable value with variable itself?
To update a MongoDB field value using its current value as part of the new value, use the $set operator with string concatenation or the $concat aggregation operator within an update pipeline. Syntax // Using aggregation pipeline (recommended) db.collection.updateMany( {}, [{ $set: { fieldName: { $concat: ["$fieldName", " additional text"] } } }] ); // Using $set with literal string (replaces with literal text) db.collection.update( {}, { $set: { fieldName: "fieldName additional text" } } ); Sample Data ...
Read MoreMongoDB bulk insert for documents
MongoDB provides multiple methods for bulk inserting documents efficiently. The most common approaches are using initializeUnorderedBulkOp() for legacy bulk operations and insertMany() for modern bulk inserts. Syntax // Legacy Bulk Operation var bulkOp = db.collection.initializeUnorderedBulkOp(); bulkOp.insert(document1); bulkOp.insert(document2); bulkOp.execute(); // Modern Approach db.collection.insertMany([document1, document2, document3]); Method 1: Using initializeUnorderedBulkOp() Create a bulk operation and insert multiple documents ? var manyDocument = db.demo255.initializeUnorderedBulkOp(); manyDocument.insert({ "Name": "Chris", "Age": 24 }); manyDocument.insert({ "Name": "Bob", "Age": 22 }); manyDocument.insert({ "Name": "David", "Age": 23 }); manyDocument.execute(); BulkWriteResult({ "writeErrors" : [ ], ...
Read MoreSplit a string during MongoDB aggregate
To split a string during MongoDB aggregation, use the $split operator which divides a string into an array based on a delimiter. For more complex operations, you can combine it with other aggregation operators like $project and $arrayElemAt. Syntax { $split: ["$fieldName", "delimiter"] } Sample Data db.splitString.insertOne({ "StudentName": "John Smith" }); { "acknowledged": true, "insertedId": ObjectId("5e0849d925ddae1f53b62206") } Method 1: Using $split in Aggregation Pipeline Split the StudentName field and extract the last name using the ...
Read MoreMongoDB query to change simple field into an object?
To change a simple field into an object in MongoDB, use the $rename operator with a temporary field approach. This involves renaming the field to a temporary name, then renaming it back with the desired object structure using dot notation. Syntax db.collection.updateMany( {}, { $rename: { "fieldName": "tempField" } } ); db.collection.updateMany( {}, { $rename: { "tempField": "objectName.nestedField" } } ); Sample Data db.changeSimpleFieldDemo.insertOne({ "StudentMarks": 58, "StudentSubject": "MySQL" ...
Read MoreMongoDB query to 'sort' and display a specific number of values
To sort and display a specific number of values in MongoDB, use the sort() method combined with limit(). The sort() method orders documents while limit() restricts the number of results returned. Syntax db.collection.find().sort({field: 1}).limit(number); Where 1 is for ascending order and -1 is for descending order. Sample Data Let us create a collection with documents ? db.demo254.insertMany([ {"Name": "Chris"}, {"Name": "Adam"}, {"Name": "Bob"} ]); { "acknowledged": true, "insertedIds": ...
Read MoreMongoDB query to fetch array values
To fetch specific array values in MongoDB, use the find() method along with $elemMatch operator to match documents containing specific values in nested arrays. Syntax db.collection.find({ "arrayField": { $elemMatch: { "nestedField": "matchValue" } } }); Sample Data db.fetchingArrayValuesDemo.insertMany([ { "StudentName": "David", ...
Read More