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
Database Articles
Page 90 of 547
Finding matching records with LIKE in MongoDB?
To perform LIKE operations in MongoDB, use regular expressions (regex) with the find() method. MongoDB doesn't have a LIKE operator like SQL, but regex patterns provide equivalent functionality for pattern matching in text fields. Syntax db.collection.find({ "field": /pattern/flags }) Common patterns: /^text/ - Starts with "text" /text$/ - Ends with "text" /text/ - Contains "text" /text/i - Case-insensitive matching Sample Data db.likeDemo.insertMany([ {"Name": "John", "Age": 32}, {"Name": "Chris", "Age": 25}, {"Name": "Carol", "Age": 22}, ...
Read MoreConverting isodate to numerical value in MongoDB?
In MongoDB, you can convert an ISODate to a numerical value (milliseconds since Unix epoch) using the getTime() method. This returns the timestamp as a number, which is useful for calculations and comparisons. Syntax yourDateVariable.getTime() Example Let's create an ISODate and convert it to a numerical value ? var arrivalDate = ISODate('2019-04-18 13:50:45'); Now convert the ISODate to numerical value ? arrivalDate.getTime(); 1555595445000 Verify Result To verify this is correct, convert the number back to ISODate ? new Date(1555595445000); ...
Read MoreCreating alias in a MongoDB query?
To create an alias in a MongoDB query, use the aggregation framework with the $project stage. This allows you to rename fields in the output by mapping original field names to new alias names. Syntax db.collection.aggregate([ { $project: { _id: 1, "aliasName": "$originalFieldName" } } ]); Sample ...
Read MoreDifference between find() and findOne() methods in MongoDB?
The findOne() method returns the first document if a query matches, otherwise returns null. The find() method never returns null — it always returns a cursor object, even when no documents match. Syntax // findOne() - returns single document or null db.collection.findOne(query, projection); // find() - returns cursor object db.collection.find(query, projection); Sample Data Let us create an empty collection to demonstrate the behavior ? db.createCollection('emptyCollection'); { "ok" : 1 } Check the document count in the collection ? db.emptyCollection.count(); 0 ...
Read MoreHow to remove a MySQL collection named 'group'?
The group is a reserved method name in MongoDB. While you can create a collection named "group", it requires special syntax to access. To remove such a collection, use getCollection('group').drop() method. Syntax db.getCollection('group').drop(); Create Sample Collection Let us first create a collection named "group" with some sample documents ? db.createCollection('group'); { "ok" : 1 } db.getCollection('group').insertMany([ {"StudentName": "Chris"}, {"StudentName": "Robert"} ]); { "acknowledged" : true, "insertedIds" : ...
Read MoreHow to apply a condition only if field exists in MongoDB?
To apply a condition only if a field exists in MongoDB, use the $or operator combined with $exists. This allows you to match documents where a field either doesn't exist or meets a specific condition. Syntax db.collection.find({ $or: [ { fieldName: { $exists: false } }, { fieldName: condition } ] }); Sample Data Let's create a collection with some documents where the StudentAge field is missing in one document ...
Read MoreRemove null element from MongoDB array?
To remove null elements from a MongoDB array, use the $pull operator. This operator removes all instances of specified values from an array field, making it perfect for eliminating null entries. Syntax db.collection.update( { /* query criteria */ }, { $pull: { "arrayField": null } } ); Sample Data Let us create a collection with documents containing null values in arrays ? db.removeNullDemo.insertMany([ { "_id": 1, ...
Read MoreWhy SHOW DBS does not show my databases in MongoDB?
The SHOW DBS command in MongoDB will not display a database until that database contains at least one collection with documents. MongoDB only allocates disk space for databases that actually contain data, which is why empty databases remain invisible in the database list. Syntax show dbs; // OR show databases; Example: Creating a Database Let's create a database named "web" and see why it doesn't appear in the database list ? use web; switched to db web Now let's check all databases ? show dbs; ...
Read MoreReturn a specific field in MongoDB?
To return a specific field in MongoDB, use the projection parameter in the find() method. Set the desired field to 1 to include it and _id to 0 to exclude the default ID field. Syntax db.collection.find({}, {fieldName: 1, _id: 0}); Sample Data db.specificFieldDemo.insertMany([ {"FirstName": "John", "LastName": "Doe"}, {"FirstName": "John", "LastName": "Smith"}, {"FirstName": "David", "LastName": "Miller"}, {"FirstName": "Sam", "LastName": "Williams"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreHow to drop a numeric collection from MongoDB?
In MongoDB, numeric collection names cannot be accessed using standard dot notation. To drop a numeric collection, use the getCollection() method followed by drop(). Syntax db.getCollection("numericCollectionName").drop(); Create Sample Data First, create a numeric collection ? db.createCollection("2536464"); { "ok" : 1 } Now insert some documents in the collection ? db.getCollection("2536464").insertMany([ {"Record": 1}, {"Record": 2}, {"Record": 3} ]); { "acknowledged" : true, "insertedIds" ...
Read More