- 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
400 Views
For this, use dot notation for field selection in MongoDB find(). Let us create a collection with documents −> db.demo302.insertOne({"Id":101, "details":[{"Name":"Chris", Age:21, "Subject":"MySQL"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d746f5d93261e4bc9ea52") } > db.demo302.insertOne({"Id":102, "details":[{"Name":"Bob", Age:23, "Subject":"MongoDB"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d74815d93261e4bc9ea53") } > db.demo302.insertOne({"Id":103, "details":[{"Name":"David", Age:20, "Subject":"Java"}]}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d74955d93261e4bc9ea54") }Display all documents from a collection with the help of find() method −> db.demo302.find();This will produce the following output −{ "_id" : ObjectId("5e4d746f5d93261e4bc9ea52"), "Id" : 101, "details" : [ { "Name" : "Chris", "Age" : 21, "Subject" : ... Read More
495 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 −{ "_id" : ObjectId("5e4d6ff55d93261e4bc9ea51"), "Name" : [ "Chris", "David", "Bob" ] }Following is the query to change the order of array elements −> db.demo301.find({}, { Name : 1 }).forEach(function(n) { ... var t = n.Name[0]; ... n.Name[0] = n.Name[1]; ... n.Name[1] = t; ... db.demo301.update({ _id: n._id ... Read More
469 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"] ... }, ... { ... "SubjectName":["Spring", "Hibernate"] ... } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4d685a5d93261e4bc9ea4b") } > > > db.demo299.insertOne( ... { ... ... Read More
220 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" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo298.save({Name: 'Mike', Marks: [88, 98] }); WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error collection: test.demo298 index: Marks_1 dup ... Read More
204 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, "insertedId" : ObjectId("5e4d54385d93261e4bc9ea43") } > db.demo297.insertOne({"Name":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d543e5d93261e4bc9ea44") } > db.demo297.insertOne({"Name":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d543e5d93261e4bc9ea45") } > db.demo297.insertOne({"Name":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d543f5d93261e4bc9ea46") } > db.demo297.insertOne({"Name":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d54405d93261e4bc9ea47") ... Read More
73 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":[ ... { ... SubjectId:[101, 103], ... "SubjectName":["MySQL", "MongoDB"] ... }, ... { ... SubjectId:[102, 104], ... "SubjectName":["Java", "C"] ... } ... ] ... ... Read More
103 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 help of find() method −> db.demo295.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e4d4ea65d93261e4bc9ea39"), "status" : [ "Active", "Inactive" ] } { "_id" : ObjectId("5e4d4eb15d93261e4bc9ea3a"), "status" : [ "Yes", "No" ] ... Read More
265 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, "insertedId" : ObjectId("5e4d4a335d93261e4bc9ea38") }Display all documents from a collection with the help of find() method −> db.demo294.find();This will produce the following output −{ "_id" : ObjectId("5e4d4a1a5d93261e4bc9ea36"), "EmployeeId" : 101, "EmployeeName" : "Chris" } { "_id" : ObjectId("5e4d4a255d93261e4bc9ea37"), "EmployeeId" : 102, "EmployeeName" : "Bob" } { "_id" : ObjectId("5e4d4a335d93261e4bc9ea38"), "EmployeeId" ... Read More
543 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") } > db.demo293.insertOne({FirstName:"Adam", LastName:"Doe", Age:21}); { "acknowledged" : true, "insertedId" : ObjectId("5e4d46cf5d93261e4bc9ea35") }Display all documents from a collection with the help of find() method −> db.demo293.find();This will produce the following output −{ "_id" : ObjectId("5e4d45075d93261e4bc9ea32"), "FirstName" : "Chris", "LastName" : "Brown", "Age" : 24 } { "_id" : ObjectId("5e4d45265d93261e4bc9ea33"), ... Read More
95 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, "insertedId" : ObjectId("5e4c10dc5d93261e4bc9ea31") }Display all documents from a collection with the help of find() method −> db.demo292.find();This will produce the following output −{ "_id" : ObjectId("5e4c10aa5d93261e4bc9ea30"), "FirstName" : "Chris", "LastName" : "Brown", "Friend" : { "FirstName" : "David", "LastName" : "Miller" } } { "_id" : ObjectId("5e4c10dc5d93261e4bc9ea31"), "FirstName" : "John", ... Read More