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 on Trending Technologies
Technical articles with clear explanations and examples
Changing the primary key on a MongoDB collection?
To change the primary key in MongoDB, you need to remove the existing _id field and let MongoDB generate a new one. Use forEach() to iterate through documents, delete the current _id, and reinsert the document with a new primary key. Syntax db.collection.find().forEach(function(doc) { var oldId = doc._id; delete doc._id; db.collection.insert(doc); db.collection.remove({_id: oldId}); }); Sample Data db.demo41.insertOne({"StudentName": "Carol"}); { "acknowledged": true, "insertedId": ObjectId("5e25ce4acfb11e5c34d898e3") } ...
Read MoreMongoDB query for documents matching array, irrespective of elements order
To query documents matching array elements irrespective of their order in MongoDB, use the $all operator. The $all operator selects documents where an array field contains all the specified elements, regardless of their position or order in the array. Syntax db.collection.find({ "arrayField": { $all: [element1, element2, ...] } }); Sample Data Let us create a collection with documents containing array fields: db.demo370.insertMany([ { "Name": "Chris", "details": [ ...
Read MoreRetrieving array values from a find query in MongoDB database
To retrieve array values from a find query in MongoDB, use dot notation to access nested fields within array elements. This allows you to query specific properties of objects stored in arrays. Syntax db.collection.find({"arrayName.fieldName": "value"}); Sample Data Let us create a collection with documents containing arrays of client details ? db.demo38.insertMany([ {"ClientDetails": [{"ClientId": 101, "ClientName": "Chris"}]}, {"ClientDetails": [{"ClientId": 102, "ClientName": "David"}]}, {"ClientDetails": [{"ClientId": 103, "ClientName": "Mike"}]} ]); { "acknowledged": true, ...
Read MoreMongoDB query to convert string to int?
To convert string to int in MongoDB, you can use parseInt() with forEach() for iteration or the $toInt operator for aggregation pipelines. The forEach() method allows you to iterate through documents and modify field types. Syntax // Method 1: Using forEach() with parseInt() db.collection.find().forEach(function(doc) { doc.fieldName = parseInt(doc.fieldName); db.collection.save(doc); }); // Method 2: Using $toInt in aggregation db.collection.aggregate([ { $addFields: { fieldName: { $toInt: "$fieldName" } } } ]); Sample Data db.demo369.insertMany([ { "Price": "1000000" }, ...
Read MoreHow to select a specific subdocument in MongoDB?
To select a specific subdocument in MongoDB, use the find() method with dot notation to query array elements and the $ positional operator in projection to return only the matched subdocument. Syntax db.collection.find( { "arrayField.subfield": "value" }, { "arrayField.$": 1, "_id": 0 } ); Sample Data Let us create a collection with documents containing subdocument arrays ? db.demo37.insertMany([ { "Details": [ ...
Read MoreMongoDB query to retrieve records from a collection named with letters and numbers
When working with MongoDB collections that have names containing letters, numbers, hyphens, and underscores, you must use the db.getCollection() method to access them, as these names cannot be used with standard dot notation. Syntax db.getCollection('collection-name-with-special-chars').find(); db.getCollection('collection-name-with-special-chars').insertOne(document); Create Sample Collection Let's create a collection with a complex name containing letters, numbers, hyphens, and underscores ? db.createCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368'); { "ok" : 1 } Insert Sample Data Insert documents into the collection using db.getCollection() ? db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').insertMany([ {"FirstName": "Chris"}, {"FirstName": "David"}, ...
Read MoreIgnore NULL and UNDEFINED values while running a MongoDB query
To ignore NULL and UNDEFINED values while querying in MongoDB, use the $ne (not equal) operator. This operator excludes documents where the specified field is null or undefined. Syntax db.collection.find({"fieldName": {$ne: null}}) Create Sample Data db.demo35.insertMany([ {"Name": "Chris"}, {"Name": null}, {"Name": "Bob"}, {"Name": undefined} ]); { "acknowledged": true, "insertedIds": [ ObjectId("5e175e42cfb11e5c34d898d0"), ...
Read MoreMongoDB query to find on the basis of true or false values
To find documents based on true or false values in MongoDB, use the $exists operator with dot notation to check field presence or use direct boolean value matching. Syntax db.collection.find({"field": { $exists: true/false }}); db.collection.find({"booleanField": true/false}); Sample Data db.demo367.insertMany([ { "Id": "102", "details": [ { "Name": "David" }, { ...
Read MoreMongoDB query to update a MongoDB row with only the objectid
To update a MongoDB document using only its ObjectId, use the update() method with the _id field as the query filter and the $set operator to modify specific fields. Syntax db.collection.update( { "_id": ObjectId("objectid_value") }, { $set: { "field1": "new_value1", "field2": "new_value2" } } ); Sample Data Let us create a collection with sample documents ? db.demo34.insertMany([ { "StudentFirstName": "Chris", "StudentAge": 24 }, { "StudentFirstName": "David", "StudentAge": 23 }, { "StudentFirstName": "Bob", ...
Read MoreMongoDB aggregation framework to sort by length of array?
To sort documents by the length of an array field in MongoDB, use the aggregation framework with $addFields and $size operators to calculate array lengths, then sort by the computed field. Syntax db.collection.aggregate([ { $addFields: { arrayLength: { $size: "$arrayField" } } }, { $sort: { arrayLength: -1 } } ]); Sample Data db.demo33.insertMany([ { "ListOfStudent": ["Chris", "Bob"] }, { "ListOfStudent": ["David", "Adam", "Mike"] }, { "ListOfStudent": ["Carol", "Sam", "John", "Robert"] } ]); ...
Read More