
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 1661 Articles for Big Data Analytics

463 Views
To update the nested document, use update() and within that, use dot notation. Let us create a collection with documents −> db.demo607.insertOne( ... { ... id:1, ... "Info1" : { ... "Name" : "Chris", ... "Age" : 21, ... ... "Info2" : { ... "SubjectName" : "MongoDB", ... "Marks" : 89 ... } ... } ... } ... ); { ... Read More

353 Views
For average in MongoDB, use the $avg. Let us create a collection with documents. Here, we have duplicate ids with rating for each −> db.demo606.insertOne({id:1, rating:5});{ "acknowledged" : true, "insertedId" : ObjectId("5e972dfbf57d0dc0b182d623") } > db.demo606.insertOne({id:1, rating:4});{ "acknowledged" : true, "insertedId" : ObjectId("5e972dfef57d0dc0b182d624") } > db.demo606.insertOne({id:2, rating:3});{ "acknowledged" : true, "insertedId" : ObjectId("5e972e09f57d0dc0b182d625") } > db.demo606.insertOne({id:1, rating:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e972e0ef57d0dc0b182d626") } > db.demo606.insertOne({id:2, rating:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e972e15f57d0dc0b182d627") } > db.demo606.insertOne({id:2, rating:3});{ "acknowledged" : true, "insertedId" : ObjectId("5e972e1bf57d0dc0b182d628") }Display all documents from a collection with the help of find() method ... Read More

195 Views
For this, use UPDATE() method and within that set the two conditions. Let us create a collection with documents −> db.demo605.insertOne( ... { ... _id:1, ... "Information" : [ ... { ... "id" : "Product-1", ... "Quantity" : 50, ... }, ... { ... "id" : "Product-2", ... "Quantity" : 100, ... }, ... Read More

3K+ Views
For this, set “unique:true” i.e. the unique constraint and avoid inserting duplicates as in the below syntax −db.yourCollectionName.ensureIndex({yourFieldName: 1}, {unique: true, dropDups: true})To understand the above syntax, let us create a collection with documents. Here, duplicate insertion won’t be allowed −> db.demo604.ensureIndex({FirstName: 1}, {unique: true, dropDups: true});{ "createdCollectionAutomatically" : true, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > db.demo604.insertOne({FirstName:"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e960887ed011c280a0905d8") } > db.demo604.insertOne({FirstName:"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e96088aed011c280a0905d9") } > db.demo604.insertOne({FirstName:"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e96088ded011c280a0905da") } > db.demo604.insertOne({FirstName:"Chris"}); 2020-04-15T00:31:35.978+0530 ... Read More

312 Views
The NumberLong(x) goes beyond its limit value and round off the value whereas NumberLong(“x”) does not.Now, we will consider a number and would use it for both NumberLong(x) and NumberLong(“x”) to see the difference.Let us create a collection with documents −> db.demo603.insert({"longValue" : NumberLong(988998985857575789)}); WriteResult({ "nInserted" : 1 }) > db.demo603.insert({"longValueInString" : NumberLong("988998985857575789")});Display all documents from a collection with the help of find() method −> db.demo603.find().pretty();This will produce the following output −{ "_id" : ObjectId("5e9605e5ed011c280a0905d1"), "longValue" : NumberLong("988998985857575808") } { "_id" : ObjectId("5e9605faed011c280a0905d2"), "longValueInString" : NumberLong("988998985857575789") }

672 Views
For such grouping of documents, use $group in MongoDB aggregate(). Let us create a collection with documents −> db.demo602.insertOne({id:1, Name:"Chris"});{ "acknowledged" : true, "insertedId" : ObjectId("5e960080ed011c280a0905c9") } > db.demo602.insertOne({id:2, Name:"David"});{ "acknowledged" : true, "insertedId" : ObjectId("5e960086ed011c280a0905ca") } > db.demo602.insertOne({id:1, Name:"Bob"});{ "acknowledged" : true, "insertedId" : ObjectId("5e96008ced011c280a0905cb") } > db.demo602.insertOne({id:3, Name:"Mike"});{ "acknowledged" : true, "insertedId" : ObjectId("5e960092ed011c280a0905cc") } > db.demo602.insertOne({id:2, Name:"John"});{ "acknowledged" : true, "insertedId" : ObjectId("5e960099ed011c280a0905cd") } > db.demo602.insertOne({id:1, Name:"Sam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e9600a1ed011c280a0905ce") }Display all documents from a collection with the help of find() method −> db.demo602.find();This will produce the following ... Read More

575 Views
Let us create a collection with documents −> db.demo601.insertOne( ... { ... id:1, ... userDetails: ... { ... userName:"John", ... userMailId:"John@gmail.com" ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e95ff5ced011c280a0905c7") } > > db.demo601.insertOne( { id:2, userDetails: { userName:"Carol", userMailId:"Carol@gmail.com" } } );{ "acknowledged" : true, "insertedId" : ObjectId("5e95ff71ed011c280a0905c8") }Display all documents from a collection with the help of find() method ... Read More

496 Views
For this, use userAdminAnyDatabase since it gives all permission like admin. Following is the syntax −use admin db.createUser( { user: "yourUserName", pwd: "yourPassword", roles: [ { role: "yourRoleName", db: "yourDatabaseName" } ] } )Let us implement the above syntax to create a user role. Following is the query to create a user with role "userAdminAnyDatabase" −> use admin switched to db admin > db.createUser( ... { ... user: "David Miller", ... pwd: "123456", ... roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] ... } ... )This will produce the following output −Successfully added user: { "user" : "David Miller", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }

484 Views
To update, use update() and following is the syntax to create and use a sample custom variable −var anyVariableName=yourValue; db.yourCollectionName.update({filter}, {$set:{yourFieldName:yourVariableName}});Let us create a collection with documents −> db.demo600.insertOne({id:1, Name:"Robert"});{ "acknowledged" : true, "insertedId" : ObjectId("5e94a063f5f1e70e134e2699") } > db.demo600.insertOne({id:2, Name:"Mike"});{ "acknowledged" : true, "insertedId" : ObjectId("5e94a06bf5f1e70e134e269a") } > db.demo600.insertOne({id:3, Name:"Sam"});{ "acknowledged" : true, "insertedId" : ObjectId("5e94a072f5f1e70e134e269b") }Display all documents from a collection with the help of find() method −> db.demo600.find();This will produce the following output −{ "_id" : ObjectId("5e94a063f5f1e70e134e2699"), "id" : 1, "Name" : "Robert" } { "_id" : ObjectId("5e94a06bf5f1e70e134e269a"), "id" : 2, "Name" : "Mike" } ... Read More

363 Views
To subtract values from document field values, use $subtract in MongoDB aggregate(). Let us create a collection with documents −> db.demo599.insertOne({"TotalPrice":250, "DiscountPrice":35});{ "acknowledged" : true, "insertedId" : ObjectId("5e948192f5f1e70e134e2696") } > db.demo599.insertOne({"TotalPrice":400, "DiscountPrice":10});{ "acknowledged" : true, "insertedId" : ObjectId("5e948199f5f1e70e134e2697") } > db.demo599.insertOne({"TotalPrice":1550, "DiscountPrice":50});{ "acknowledged" : true, "insertedId" : ObjectId("5e9481a0f5f1e70e134e2698") }Display all documents from a collection with the help of find() method −> db.demo599.find();This will produce the following output −{ "_id" : ObjectId("5e948192f5f1e70e134e2696"), "TotalPrice" : 250, "DiscountPrice" : 35 } { "_id" : ObjectId("5e948199f5f1e70e134e2697"), "TotalPrice" : 400, "DiscountPrice" : 10 } { "_id" : ObjectId("5e9481a0f5f1e70e134e2698"), "TotalPrice" : 1550, "DiscountPrice" ... Read More