- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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 1349 Articles for MongoDB
207 Views
Let us first create a collection with documents −> db.demo186.insertOne({"UserEmailId":"JOHN@GMAIL.COM", "UserName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d769e4f06af55199808") } > db.demo186.insertOne({"UserEmailId":"chris@gmail.com", "UserName":"chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d879e4f06af55199809") } > db.demo186.insertOne({"UserEmailId":"DAVID@GMAIL.COM", "UserName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e399d979e4f06af5519980a") }Display all documents from a collection with the help of find() method −> db.demo186.find();This will produce the following output −{ "_id" : ObjectId("5e399d769e4f06af55199808"), "UserEmailId" : "JOHN@GMAIL.COM", "UserName" : "John" } { "_id" : ObjectId("5e399d879e4f06af55199809"), "UserEmailId" : "chris@gmail.com", "UserName" : "chris" } { "_id" : ObjectId("5e399d979e4f06af5519980a"), "UserEmailId" : "DAVID@GMAIL.COM", "UserName" : "David" }Following is the ... Read More
65 Views
To fetch records with $or on the basis of existence, use $or along with $exists. Let us create a collection with documents −>db.demo185.insertOne({_id:101, details:{Name:"Chris", Score:78, Subjects:{"Name":"MySQL"}}}); { "acknowledged" : true, "insertedId" : 101 } > db.demo185.insertOne({_id:102, details:{Name:"Bob", Score:78}}); { "acknowledged" : true, "insertedId" : 102 } >db.demo185.insertOne({_id:103, details:{Name:"David", Score:78, Subjects:{"Name":"MongoDB"}}}); { "acknowledged" : true, "insertedId" : 103 }Display all documents from a collection with the help of find() method −> db.demo185.find();This will produce the following output −{ "_id" : 101, "details" : { "Name" : "Chris", "Score" : 78, "Subjects" : { "Name" : "MySQL" } } } { "_id" ... Read More
721 Views
To count, you can also use aggregate() along with $sum. Let us create a collection with documents −> db.demo184.insertOne({"Names":["Chris", "David", "Bob"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3999fb9e4f06af55199805") } > db.demo184.insertOne({"Names":["Chris", "Mike"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e399a0d9e4f06af55199806") } > db.demo184.insertOne({"Names":["Chris", "Bob", "Carol"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e399a209e4f06af55199807") }Display all documents from a collection with the help of find() method −> db.demo184.find();This will produce the following output −{ "_id" : ObjectId("5e3999fb9e4f06af55199805"), "Names" : [ "Chris", "David", "Bob" ] } { "_id" : ObjectId("5e399a0d9e4f06af55199806"), "Names" : [ "Chris", "Mike" ] } { ... Read More
222 Views
To display subdocuments in one line, use $unwind along with aggregate(). Let us create a collection with documents −> db.demo183.insertOne( ... { ... "_id": "110", ... "DueDate": ISODate("2020-02-04T01:10:42.000Z"), ... "ProductDetails": [ ... { ... "ProductName": "Product-1", ... "isAvailable": true ... }, ... { ... "ProductName": "Product-2", ... "isAvailable": false ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : "110" }Display all documents from a collection with the ... Read More
1K+ Views
To select one field if the other is null, use $ifNull. Let us create a collection with documents −> db.demo182.insertOne({"FirstName":"Chris", "LastName":null}); { "acknowledged" : true, "insertedId" : ObjectId("5e398ea19e4f06af55199802") } > db.demo182.insertOne({"FirstName":null, "LastName":"Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5e398ead9e4f06af55199803") } > > db.demo182.insertOne({"FirstName":"John", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e398ebf9e4f06af55199804") }Display all documents from a collection with the help of find() method −> db.demo182.find();This will produce the following output −{ "_id" : ObjectId("5e398ea19e4f06af55199802"), "FirstName" : "Chris", "LastName" : null } { "_id" : ObjectId("5e398ead9e4f06af55199803"), "FirstName" : null, "LastName" : "Miller" } { "_id" ... Read More
631 Views
To search using month and day only, use $where. Let us create a collection with documents −> db.demo181.insertOne({"ShippingDate":new ISODate("2020-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e398a699e4f06af551997fe") } > db.demo181.insertOne({"ShippingDate":new ISODate("2019-12-11")}); { "acknowledged" : true, "insertedId" : ObjectId("5e398a729e4f06af551997ff") } > db.demo181.insertOne({"ShippingDate":new ISODate("2018-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e398a7d9e4f06af55199800") } > db.demo181.insertOne({"ShippingDate":new ISODate("2020-10-12")}); { "acknowledged" : true, "insertedId" : ObjectId("5e398a879e4f06af55199801") }Display all documents from a collection with the help of find() method −> db.demo181.find();This will produce the following output −{ "_id" : ObjectId("5e398a699e4f06af551997fe"), "ShippingDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5e398a729e4f06af551997ff"), "ShippingDate" : ... Read More
240 Views
To match documents with fields not containing values in array, use $nin. Let us create a collection with documents −> db.demo180.insertOne({"Scores":["80", "90", "110"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3988a69e4f06af551997fb") } > db.demo180.insertOne({"Scores":["110", "70", "60"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3988b79e4f06af551997fc") } > db.demo180.insertOne({"Scores":["40", "70", "1010"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e3988cc9e4f06af551997fd") }Display all documents from a collection with the help of find() method −> db.demo180.find();This will produce the following output −{ "_id" : ObjectId("5e3988a69e4f06af551997fb"), "Scores" : [ "80", "90", "110" ] } { "_id" : ObjectId("5e3988b79e4f06af551997fc"), "Scores" : [ "110", "70", ... Read More
72 Views
To find latest entries in array over all document, use aggregate(). Let us create a collection with documents −> db.demo179.insertOne( ...{ ... "Name":"Chris", ... "Details": [ ... { ... "Id":101, ... "Subject":"MongoDB" ... }, ... { ... "Id":102, ... "Subject":"MySQL" ... } ... ] ...} ...); { "acknowledged" : true, "insertedId" : ObjectId("5e3980299e4f06af551997f9") } > db.demo179.insertOne( ...{ ... "Name":"David", ... "Details": [ ... { ... "Id":103, ... "Subject":"Java" ... }, ... { ... ... Read More
267 Views
Let us create a collection with documents −> db.demo178.insertOne({"DueDate":new ISODate("2019-01-10T06:18:20.474Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5e397bd89e4f06af551997f5") } > db.demo178.insertOne({"DueDate":new ISODate("2020-11-10T18:05:11.474Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5e397bf39e4f06af551997f6") } > db.demo178.insertOne({"DueDate":new ISODate("2020-03-15T07:05:10.474Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5e397c039e4f06af551997f7") } > db.demo178.insertOne({"DueDate":new ISODate("2020-06-11T16:05:10.474Z")}); { "acknowledged" : true, "insertedId" : ObjectId("5e397c0f9e4f06af551997f8") }Display all documents from a collection with the help of find() method −> db.demo178.find();This will produce the following output −{ "_id" : ObjectId("5e397bd89e4f06af551997f5"), "DueDate" : ISODate("2019-01-10T06:18:20.474Z") } { "_id" : ObjectId("5e397bf39e4f06af551997f6"), "DueDate" : ISODate("2020-11-10T18:05:11.474Z") } { "_id" : ObjectId("5e397c039e4f06af551997f7"), "DueDate" : ISODate("2020-03-15T07:05:10.474Z") ... Read More
119 Views
To add a document in an already created collection, use $push in MongoDB. Let us create a collection with documents −> db.demo177.insertOne( { "Id": "101", "details": [ { "StudentName": "Chris", "Scores": [67, 71, 74], "SubjectName": ["MySQL", "Java"] }, { "StudentName": "David", "Scores": [89, 98, 45], "SubjectName": ["PL/SQL", "C"] } ] } ); { "acknowledged" : true, "insertedId" : ObjectId("5e384b2b9e4f06af551997f4") }Display all documents from a collection with the help of find() method −> db.demo177.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e384b2b9e4f06af551997f4"), "Id" : "101", "details" ... Read More