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
MongoDB Articles
Page 37 of 111
Query in MongoDB to perform an operation similar to LIKE operation
MongoDB doesn't have a direct LIKE operator, but you can achieve similar functionality using regular expressions with the /pattern/ syntax or the $regex operator for pattern matching in string fields. Syntax // Using regex pattern db.collection.find({"field": /pattern/}) // Using $regex operator db.collection.find({"field": {$regex: "pattern"}}) Sample Data db.demo26.insertMany([ {"StudentName": "Chris"}, {"StudentName": "John"}, {"StudentName": "Jones"}, {"StudentName": "David"} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreQuerying from part of object in an array with MongoDB
To query from part of object in an array with MongoDB, use dot notation to access nested array fields and the $all operator to match documents where an array field contains all specified values. Syntax db.collection.find({ "arrayField.nestedField": { $all: ["value1", "value2"] } }); Sample Data Let us create a collection with documents containing user details in arrays ? db.demo25.insertMany([ { "Details": [ { ...
Read MoreMongoDB query to push a computed expression in a $group?
To push a computed expression in MongoDB's $group stage, use the $push operator combined with conditional expressions like $cond. This allows you to transform field values during the grouping process and collect them into arrays. Syntax db.collection.aggregate([ { $group: { _id: "$fieldName", arrayField: { $push: ...
Read MoreSorting field value (FirstName) for MongoDB?
To sort field values in MongoDB, use the sort() method with field name and sort direction. Use 1 for ascending order and -1 for descending order. Syntax db.collection.find().sort({ "fieldName": 1 }); // Ascending db.collection.find().sort({ "fieldName": -1 }); // Descending Sample Data db.demo365.insertMany([ { "FirstName": "Chris" }, { "FirstName": "Adam" }, { "FirstName": "John" }, { "FirstName": "Bob" } ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreMongoDB query to find records with keys containing dots?
To find MongoDB records containing field names with dots, use the aggregation pipeline with $addFields, $objectToArray, and $filter operators. The $indexOfBytes operator detects dot characters in field names. Syntax db.collection.aggregate([ { $addFields: { result: { $filter: { ...
Read MoreFetch multiple documents in MongoDB query with OR condition?
To fetch multiple documents in MongoDB using OR condition, use the $or operator which performs a logical OR operation on an array of expressions and selects documents that satisfy at least one of the expressions. Syntax db.collection.find({ $or: [ { field1: value1 }, { field2: value2 }, { field3: value3 } ] }); Sample Data db.demo362.insertMany([ { ...
Read MoreUsing object as key for finding values in MongoDB
To use an object field as a key for finding values in MongoDB, use dot notation to access nested object properties in the projection parameter of the find() method. Syntax db.collection.find({}, {"objectName.fieldName": 1}); Sample Data Let us create a collection with nested object documents − db.demo361.insertMany([ {"details": {"FirstName": "Chris", "LastName": "Brown"}}, {"details": {"FirstName": "David", "LastName": "Miller"}}, {"details": {"FirstName": "John", "LastName": "Doe"}} ]); { "acknowledged": true, "insertedIds": [ ...
Read MoreFind current and previous documents in MongoDB
To get the current and previous documents in MongoDB, use sort() with limit() to retrieve the most recent documents based on a specific criteria. This approach helps you find the latest two records that match your query conditions. Syntax db.collection.find( { field: { $lte: value } }, { projection } ).sort({ _id: -1 }).limit(2); Sample Data Let us create a collection with sample documents: db.demo360.insertMany([ { id: 101, "Name": "Chris" }, { id: 102, "Name": "David" ...
Read MoreMongoDB query to concatenate values of array with other fields
To concatenate values of an array with other fields in MongoDB, use $reduce to convert the array into a single string, then $concat to combine it with other fields in the $project stage. Syntax db.collection.aggregate([ { $project: { arrayAsString: { $reduce: { ...
Read MoreDoes aggregation query with $match works in MongoDB?
Yes, the $match operator works perfectly in MongoDB aggregation queries. It filters documents in the aggregation pipeline, similar to the find() method's query criteria, but as the first stage of data processing. Syntax db.collection.aggregate([ { $match: { "field": "value" } }, // Additional pipeline stages... ]); Sample Data db.demo358.insertMany([ { "ClientId": 101, "ClientName": "Chris", "ClientAge": 34 }, { "ClientId": 102, "ClientName": "David", "ClientAge": 32 }, { "ClientId": 103, "ClientName": "David", "ClientAge": 35 }, ...
Read More