Database Articles

Page 98 of 547

How to count the number of documents in a MongoDB collection?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 656 Views

To count the number of documents in a MongoDB collection, use the countDocuments() method or the legacy count() method. The countDocuments() method is recommended for accurate results. Syntax db.collectionName.countDocuments(); db.collectionName.countDocuments({query}); Create Sample Data Let us first create a collection with documents ? db.countNumberOfDocumentsDemo.insertMany([ {"CustomerName": "Bob"}, {"CustomerName": "Ramit", "CustomerAge": 23}, {"CustomerName": "Adam", "CustomerAge": 27, "CustomerCountryName": "US"} ]); { "acknowledged": true, "insertedIds": [ ...

Read More

How to return only a single property "_id" in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 2K+ Views

To return only the _id property in MongoDB, use the projection parameter in the find() method by setting {"_id": 1}. This filters the output to show only the _id field from all matching documents. Syntax db.collectionName.find({}, {"_id": 1}); Sample Data Let us create a collection with sample documents : db.singlePropertyIdDemo.insertMany([ {"_id": 101, "UserName": "Larry", "UserAge": 21}, {"_id": 102, "UserName": "Mike", "UserAge": 26}, {"_id": 103, "UserName": "Chris", "UserAge": 24}, {"_id": 104, "UserName": "Robert", "UserAge": 23}, ...

Read More

Is it possible to rename _id field after MongoDB group aggregation?

George John
George John
Updated on 15-Mar-2026 2K+ Views

Yes, it is possible to rename the _id field after MongoDB group aggregation using the $project stage. This technique allows you to map the _id field to a new field name while excluding the original _id from the output. Syntax db.collection.aggregate([ { $project: { _id: 0, newFieldName: "$_id", otherFields: ...

Read More

Query MongoDB with length criteria?

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

To query MongoDB with length criteria, you can use the $regex operator with regular expressions to match string fields based on their character length. Syntax db.collection.find({ "fieldName": { $regex: /^.{minLength, maxLength}$/ } }); Where minLength and maxLength define the character count range. Sample Data db.queryLengthDemo.insertMany([ {"StudentFullName": "John Smith"}, {"StudentFullName": "John Doe"}, {"StudentFullName": "David Miller"}, {"StudentFullName": "Robert Taylor"}, {"StudentFullName": "Chris Williams"} ]); { ...

Read More

How to iterate over all MongoDB databases?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 440 Views

To iterate over all MongoDB databases, you need to switch to the admin database and use the listDatabases command. This returns information about all databases in the MongoDB instance. Syntax // Switch to admin database switchDatabaseAdmin = db.getSiblingDB("admin"); // Get all database information allDatabaseName = switchDatabaseAdmin.runCommand({ "listDatabases": 1 }).databases; Example Here's how to get information about all databases ? switchDatabaseAdmin = db.getSiblingDB("admin"); allDatabaseName = switchDatabaseAdmin.runCommand({ "listDatabases": 1 }).databases; This will produce the following output ? [ { ...

Read More

Upsert in MongoDB while using custom _id values to insert a document if it does not exist?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 666 Views

To perform an upsert with custom _id values in MongoDB, use update() with the upsert option instead of insert(). When you use insert() with existing _id values, MongoDB throws a duplicate key error. The upsert operation inserts a document if it doesn't exist or updates it if it does. Syntax db.collection.update( { "_id": customIdValue }, { $set: { field1: "value1", field2: "value2" } }, { upsert: true } ); Sample Data First, let's create a collection and demonstrate the duplicate key error ...

Read More

How can I to know if my database MongoDB is 64 bits?

George John
George John
Updated on 15-Mar-2026 185 Views

You can use buildInfo along with runCommand to check whether your MongoDB database is running on 32-bit or 64-bit architecture. The key field to look for in the output is "bits". Syntax use admin db.runCommand("buildInfo") Example First, switch to the admin database and then run the buildInfo command ? use admin db.runCommand("buildInfo"); switched to db admin The output will display detailed build information about your MongoDB instance ? { "version" : "4.0.5", "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412", ...

Read More

Access objects from the nested objects structure in MongoDB

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

To access objects from nested objects structure in MongoDB, use dot notation to traverse through multiple levels of nested documents. This allows you to query and retrieve documents based on deeply nested field values. Syntax db.collection.find({ "parentObject.childObject.nestedField": "value" }); Create Sample Data Let us first create a collection with nested documents ? db.nestedObjectDemo.insertOne({ "Student": { "StudentDetails": { "StudentPersonalDetails": { ...

Read More

Search multiple fields for multiple values in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 15-Mar-2026 779 Views

To search multiple fields for multiple values in MongoDB, you can use the $text and $search operators. This requires creating a text index on the fields you want to search across. Syntax db.collection.createIndex({ "field1": "text", "field2": "text" }); db.collection.find({ "$text": { "$search": "value1 value2" } }); Sample Data db.searchMultipleFieldsDemo.insertMany([ {"_id": 100, "FirstSubject": "Java", "SecondSubject": "MongoDB"}, {"_id": 101, ...

Read More

Find all the non-distinct values of a field in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 15-Mar-2026 1K+ Views

To find all non-distinct values of a field in MongoDB, use the aggregate() method with $group to count occurrences and $match to filter values that appear more than once. Syntax db.collection.aggregate([ { $group: { "_id": "$fieldName", "count": { $sum: 1 } }}, { $match: { "count": { $gt: 1 } }} ]); Sample Data ...

Read More
Showing 971–980 of 5,468 articles
« Prev 1 96 97 98 99 100 547 Next »
Advertisements