Chandu yadav

Chandu yadav

810 Articles Published

Articles by Chandu yadav

Page 29 of 81

How to find and modify a value in a nested array?

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

To find and modify a value in a nested array, use the $elemMatch operator to locate the array element and the $ positional operator with $set to update the specific field value. Syntax db.collection.update( { arrayName: { $elemMatch: { field: "matchValue" } } }, { $set: { "arrayName.$.targetField": "newValue" } } ); Sample Data db.findAndModifyAValueInNestedArrayDemo.insertOne({ "CompanyName": "Amazon", "DeveloperDetails": [ { ...

Read More

How do I find all documents with a field that is NaN in MongoDB?

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

To find all documents with a field that is NaN in MongoDB, use the $eq operator or direct comparison with NaN value in your query. Syntax db.collection.find({ fieldName: NaN }); Or using the $eq operator: db.collection.find({ fieldName: { $eq: NaN } }); Create Sample Data First, let's create a collection with documents containing various numerical values including NaN ? db.nanDemo.insertMany([ { "Score": 0/0 }, // NaN { "Score": 10/5 }, ...

Read More

Using find() to search for nested keys in MongoDB?

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

To search for nested keys in MongoDB, use dot notation to specify the path to the nested field. This allows you to query documents based on values within embedded objects. Syntax db.collectionName.find({"outerField.innerField": "value"}); Sample Data Let us create a collection with nested documents ? db.searchForNestedKeysDemo.insertMany([ { "ClientName": "Larry", "ClientAge": 28, "ClientExtraDetails": { ...

Read More

Query MongoDB for a datetime value less than NOW?

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

To query MongoDB for datetime values less than the current time, use the $lte operator with new Date() which returns the current timestamp. Syntax db.collection.find({ "dateField": { $lte: new Date() } }); Sample Data db.dateTimeValueLessThanNowDemo.insertMany([ { "CustomerName": "Larry", "CustomerProductName": "Product-1", "ArrivalDate": new ISODate("2017-01-31") }, { ...

Read More

Building Multiple Indexes at once in MongoDB?

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

To build multiple indexes at once in MongoDB, use the createIndexes() method and pass multiple index specifications in an array. This approach is more efficient than creating indexes one by one. Syntax db.collection.createIndexes([ { "field1": 1 }, { "field2": -1 }, { "field3": 1, "field4": 1 } ]); Example Let's create multiple single-field indexes on a collection ? db.multipleIndexesDemo.createIndexes([ {"First": 1}, {"Second": 1}, {"Third": 1}, ...

Read More

How do you limit an array sub-element in MongoDB?

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

You can use $slice operator to limit array elements in MongoDB query results. The $slice operator controls how many elements from an array field are returned in the projection. Syntax db.collection.find( {}, { "arrayField": { $slice: N } } ) Where N can be: Positive number: Returns first N elements Negative number: Returns last N elements [skip, limit]: Skips elements and limits result Sample Data db.limitAnArrayDemo.insertOne({ _id: 101, "PlayerName": "Bob", ...

Read More

Get component of Date / ISODate in MongoDB?

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

To get component of Date/ISODate in MongoDB, you can access individual date components like year, month, day, hour, minute, and second using JavaScript date methods on the ISODate field. Syntax // Access date field var dateField = db.collection.findOne().dateFieldName; // Get components dateField.getFullYear() // Year (e.g., 2019) dateField.getMonth() // Month (0-11, January=0) dateField.getDate() // Day of month (1-31) dateField.getHours() // Hour (0-23) dateField.getMinutes() // Minutes (0-59) dateField.getSeconds() ...

Read More

Check if MongoDB database exists?

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

To check if a MongoDB database exists, use the indexOf() method on the list of database names. This returns the index position if the database exists, or -1 if it doesn't exist. Syntax db.getMongo().getDBNames().indexOf("databaseName"); Return Values Positive number (0 or greater): Database exists at that index position -1: Database does not exist Case 1: Database Exists Check if the "test" database exists ? db.getMongo().getDBNames().indexOf("test"); 6 The result 6 means the "test" database exists and is located at index 6 in the database list. ...

Read More

Get MongoDB Databases in a JavaScript Array?

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

To get MongoDB databases in a JavaScript array, you can use the listDatabases command with runCommand() and then extract database names into an array using JavaScript. Syntax use admin; allDatabasesDetails = db.runCommand({listDatabases: 1}); databaseNames = allDatabasesDetails.databases.map(db => db.name); Step 1: Get All Databases Details First, switch to the admin database and run the listDatabases command ? use admin; allDatabasesDetails = db.runCommand({listDatabases: 1}); { "databases" : [ { "name" : "admin", ...

Read More

MongoDB equivalent of SELECT field AS `anothername`?

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

In MySQL, we give an alias name for a column. Similarly, you can give an alias name for field name in MongoDB using the $project stage in aggregation pipeline. Syntax db.collectionName.aggregate([ { "$project": { "_id": 0, "aliasName": "$originalFieldName" }} ]); Sample Data Let us first create a collection with documents ? db.selectFieldAsAnotherNameDemo.insertMany([ {"Name": "Larry"}, {"Name": "Robert"}, ...

Read More
Showing 281–290 of 810 articles
« Prev 1 27 28 29 30 31 81 Next »
Advertisements