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
AmitDiwan
623 Views
Use swap concept to change the order of array elements. Let us create a collection with documents −> db.demo301.insertOne({"Name":["Chris", "David", "Bob"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d6ff55d93261e4bc9ea51") }Display all documents from a collection with the help of find() method −> db.demo301.find();This will produce the following output −{ ... Read More
AmitDiwan
411 Views
For exact array matching, simply use find() in MongoDB. Let us create a collection with documents −> db.demo300.insertOne({"Values":[100, 200, 400]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d69d05d93261e4bc9ea4d") } > db.demo300.insertOne({"Values":[500, 700, 900, 1000]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d69dd5d93261e4bc9ea4e") } > db.demo300.insertOne({"Values":[340, 670, 450, 500]}); ... Read More
AmitDiwan
570 Views
To access an object in an array, use dot notation. Let us create a collection with documents −> db.demo299.insertOne( ... { ... "id":100, ... "Name":"Robert", ... "details":[ ... { ... "SubjectName":["C++", "Python"] ... ... Read More
AmitDiwan
321 Views
Use ensureIndex() and set unique:1 inside the array. Let us create a collection with documents −> db.demo298.save({Name: 'Chris', Marks: [46, 79] }); WriteResult({ "nInserted" : 1 }) > db.demo298.save({Name: 'David', Marks: [67, 88] }); WriteResult({ "nInserted" : 1 }) > db.demo298.ensureIndex({ Marks: 1 }, {unique: 1}); { "createdCollectionAutomatically" : ... Read More
AmitDiwan
274 Views
To insert and limit the total records while inserting, use capped:true and set the size and max values.Let us create a collection with documents wherein we have set capped:true and size to 4 −> db.createCollection("demo297", {capped:true, size:4, max:4}); { "ok" : 1 } > db.demo297.insertOne({"Name":"Chris"}); { "acknowledged" : true, ... Read More
AmitDiwan
144 Views
To query inside array and check for existence to get the count, use $exists. Let us create a collection with documents −> db.demo296.insertOne( ... { ... "id":101, ... "Name":"Chris", ... "details":[ ... { ... ... Read More
AmitDiwan
193 Views
Query object’s field array value using arrayFieldName along with value. Let us create a collection with documents −> db.demo295.insertOne({"status":["Active", "Inactive"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4ea65d93261e4bc9ea39") } > db.demo295.insertOne({"status":["Yes", "No"]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4eb15d93261e4bc9ea3a") }Display all documents from a collection with the ... Read More
AmitDiwan
353 Views
To query or return a MongoDB collection, use getCollection(). Let us create a collection with documents −> db.demo294.insertOne({"EmployeeId":101, "EmployeeName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a1a5d93261e4bc9ea36") } > db.demo294.insertOne({"EmployeeId":102, "EmployeeName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d4a255d93261e4bc9ea37") } > db.demo294.insertOne({"EmployeeId":103, "EmployeeName":"David"}); { "acknowledged" : true, ... Read More
AmitDiwan
677 Views
Let us first create a collection with documents −> db.demo293.insertOne({FirstName:"Chris", LastName:"Brown", Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d45075d93261e4bc9ea32") } > db.demo293.insertOne({FirstName:"David", LastName:"Miller", Age:23}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d45265d93261e4bc9ea33") } > db.demo293.insertOne({FirstName:"John", LastName:"Smith", Age:24}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d45385d93261e4bc9ea34") } ... Read More
AmitDiwan
143 Views
For this, check using $where in MongoDB. Let us create a collection with documents −> db.demo292.insertOne({FirstName:"Chris", LastName:"Brown", ... "Friend":{FirstName:"David", "LastName":"Miller"} ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4c10aa5d93261e4bc9ea30") } > db.demo292.insertOne({FirstName:"John", LastName:"Doe", ... "Friend":{FirstName:"Mike", "LastName":"Doe"} ...} ...); { "acknowledged" : true, ... Read More