
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Found 6705 Articles for Database

2K+ Views
To find records on or after a date, use $gte i.e., greater than equal. Let us create a collection with documents −> db.demo91.insertOne({"ArrivalDate":new ISODate("2020-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e2d49fd79799acab037af66") } > db.demo91.insertOne({"ArrivalDate":new ISODate("2019-12-14")}); { "acknowledged" : true, "insertedId" : ObjectId("5e2d4a0679799acab037af67") }Display all documents from a collection with the help of find() method −> db.demo91.find();This will produce the following output −{ "_id" : ObjectId("5e2d49fd79799acab037af66"), "ArrivalDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5e2d4a0679799acab037af67"), "ArrivalDate" : ISODate("2019-12-14T00:00:00Z") }Following is the query to find records on or after a specific date in MongoDB −> db.demo91.find({ArrivalDate: { $gte: ISODate('2020-01-10') ... Read More
MongoDB query to find documents whose array contains a string that is a substring of a specific word

767 Views
For such evaluations, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo90.insertOne( ... {"words": ["john", "jace"] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c1ada79799acab037af56") } > db.demo90.insertOne( ... {"words": ["sam", "adam"] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c1adb79799acab037af57") }Display all documents from a collection with the help of find() method −> db.demo90.find();This will produce the following output −{ "_id" : ObjectId("5e2c1ada79799acab037af56"), "words" : [ "john", "jace" ] } { "_id" : ObjectId("5e2c1adb79799acab037af57"), "words" : [ "sam", "adam" ] }Following is the query to find documents ... Read More

299 Views
To get array of nested string in MongoDB, use dot notation in find(). Let us create a collection with documents −> db.demo89.insertOne( ... { id: 101, Details: [ { Name: "Chris", Marks: 45 }, { Name: "David", Marks: 55, Subjects : ["MySQL", "MongoDB", "Java", "C"] } ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c163b79799acab037af51") } > db.demo89.insertOne( ... { id: 102, Details: [ { Name: "Mike", Marks: 48 }, { Name: "Bob", Marks: 98, Subjects : ["C++", "MySQL"] } ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c163c79799acab037af52") }Display ... Read More

118 Views
Yes, you can use $indexOfCP to use a filed value as a pattern. Let us create a collection with documents −> db.demo88.insertOne( ... { ... "Name": "Chris", ... "PassoutYear": "2020", ... "websiteName": "chris.shop.com/Carol-2020-" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c14c471bf0181ecc422b1") } > db.demo88.insertOne( ... { ... "Name": "David", ... "PassoutYear": "2010", ... "websiteName": "david.bigshop.com/David-2010-" ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c14c571bf0181ecc422b2") }Display all documents from a ... Read More

166 Views
To filter by several array elements, use $elemMatch. Let us create a collection with documents −> db.demo87.insertOne( ... { ... id:101, ... "Details": [ ... { ... "EmployeeName": "Chris", ... "Salary": 45000 ... }, ... { ... "EmployeeName": "David", ... "Salary": 50000 ... } ... ] ... } ... ); { "acknowledged" ... Read More

825 Views
For this, use $not along with $in. Let us create a collection with documents −[ { id: "101", subjectid: [ "102" ] }, { id: "102", subjectid: [ "102" ] } ]Here is the snapshot.Display all documents from a collection with the help of find() method −db.collection.find()This will produce the following output −[ { "_id": ObjectId("5a934e000102030405000000"), "id": "101", "subjectid": [ "102" ] }, { "_id": ObjectId("5a934e000102030405000001"), "id": "102", "subjectid": [ "102" ] } ]Following is the query that uses $expr, $not and $in to fetch values excluding matching field array value −db.collection.find({ $expr: { $not:{ $in: [ "$id", "$subjectid" ] } } })This will produce the following output −[ { "_id": ObjectId("5a934e000102030405000000"), "id": "101", "subjectid": [ "102" ] } ]

1K+ Views
To match multiple criteria inside an array, use aggregate(). Let us create a collection with documents −> db.demo84.insertOne({ ... "EmployeeDetails": [ ... {Name: 'John', Salary:45000, isMarried: true}, ... {Name: 'Chris', Salary:50000, isMarried: false} ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e2c0a3471bf0181ecc422a5") } > db.demo84.insertOne({ ... "EmployeeDetails": [ ... {Name: 'Sam', Salary:56000, isMarried: false}, ... {Name: 'Bob', Salary:50000, isMarried: false} ... ] ... } ... ); { "acknowledged" : true, ... Read More

472 Views
To fetch a document, use $in, instead of $and in MongoDB. Let us first create a collection with documents −> db.demo83.insertOne( ... { ... "Details":[ ... { ... "Name":"Chris", ... "Subject":[ ... "MySQL", ... "MongoDB" ... ] ... }, ... { ... "Name":"David", ... "Subject":[ ... "Java", ... "C" ... ... Read More

1K+ Views
To make a unique field in MongoDB, use unique − true. Let us create a collection with documents −> db.demo82.createIndex({"EmployeeName":1}, {unique:true}); { "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo82.insertOne({"EmployeeName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bfb1b71bf0181ecc422a0") } > db.demo82.insertOne({"EmployeeName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bfb1f71bf0181ecc422a1") } > db.demo82.insertOne({"EmployeeName":"Chris"}); 2020-01-25T13:54:00.410+0530 E QUERY [js] WriteError: E11000 duplicate key error collection: test.demo82 index: EmployeeName_1 dup key: { : "Chris" } : WriteError({ "index" : 0, "code" : 11000, "errmsg" : "E11000 duplicate key ... Read More

152 Views
To get a specific item in a sudocument, use the dot(.) notation. Let us create a collection with documents −> db.demo81.insertOne({"StudentDetails":[{"StudentName":"Carol", "StudentSubject":"Java"}, { "StudentName" : "David", "StudentSubject" : "MongoDB" }]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf6ec71bf0181ecc4229d") } > db.demo81.insertOne({"StudentDetails":[{"StudentName":"Mike", "StudentSubject":"Python"}, { "StudentName" : "David", "StudentSubject" : "C" }]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf70471bf0181ecc4229e") } > db.demo81.insertOne({"StudentDetails":[{"StudentName":"Jace", "StudentSubject":"C++"}, { "StudentName" : "John", "StudentSubject" : "MySQL" }]}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf72071bf0181ecc4229f") }Display all documents from a collection with the help of find() method −> db.demo81.find();This will produce the following ... Read More