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
AmitDiwan has Published 10740 Articles
MongoDB query to find documents whose array contains a string that is a substring of a specific word
AmitDiwan
796 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, ... Read More
AmitDiwan
316 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"] } ] ... } ... ... Read More
AmitDiwan
133 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-" ... } ... ); { ... Read More
AmitDiwan
200 Views
To filter by several array elements, use $elemMatch. Let us create a collection with documents −> db.demo87.insertOne( ... { ... id:101, ... "Details": [ ... { ... "EmployeeName": "Chris", ... ... Read More
AmitDiwan
861 Views
For this, use $not along with $in. Let us create a collection with documents −[ { id: "101", subjectid: [ "102" ] }, { id: "102", subjectid: [ ... Read More
AmitDiwan
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} ... ] ... } ... ); ... Read More
AmitDiwan
489 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":[ ... ... Read More
AmitDiwan
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") ... Read More
AmitDiwan
173 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" ... Read More
AmitDiwan
537 Views
To calculate average in MongoDB, use $avg. Let us create a collection with documents −> db.demo80.insertOne({"Details":{"Price":10.5}}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf43271bf0181ecc42297") } > db.demo80.insertOne({"Details":{"Price":50.3}}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf43871bf0181ecc42298") } > db.demo80.insertOne({"Details":{"Price":100.10}}); { "acknowledged" : true, "insertedId" : ObjectId("5e2bf43f71bf0181ecc42299") }Display ... Read More