

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Randomizing unique data with MongoDB and placing values for emailid with word John in the beginning
To randomize unique data, use Math.random() in MongoDB. Let us create a collection with documents −
> db.demo561.insertOne({EmailId:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f490454b4472ed3e8e86c") } > db.demo561.insertOne({EmailId:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f490654b4472ed3e8e86d") } > db.demo561.insertOne({EmailId:null});{ "acknowledged" : true, "insertedId" : ObjectId("5e8f490a54b4472ed3e8e86e") }
Display all documents from a collection with the help of find() method −
> db.demo561.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f490454b4472ed3e8e86c"), "EmailId" : null } { "_id" : ObjectId("5e8f490654b4472ed3e8e86d"), "EmailId" : null } { "_id" : ObjectId("5e8f490a54b4472ed3e8e86e"), "EmailId" : null }
Following is the query for randomizing unique data with MongoDB −
> db.demo561.find().forEach(function(doc){ ... db.demo561.update({_id : doc._id}, {$set:{ ... EmailId:'John'+Math.random()*100000000000000000+'@'+Math.random()*100000000000000000+'.com' ... }}) ... })
Display all documents from a collection with the help of find() method −
> db.demo561.find();
This will produce the following output −
{ "_id" : ObjectId("5e8f490454b4472ed3e8e86c"), "EmailId" : "John23607829153155868@62688631475897960.com" } { "_id" : ObjectId("5e8f490654b4472ed3e8e86d"), "EmailId" : "John63351292234094710@79460595429439740.com" } { "_id" : ObjectId("5e8f490a54b4472ed3e8e86e"), "EmailId" : "John71315584787457890@99884571221675000.com" }
- Related Questions & Answers
- Get Distinct Values with Sorted Data in MongoDB?
- Find the frequency of unique values and missing values for each column in an R data frame.
- Query for values (not objects) in list with MongoDB
- Get the last two values with MongoDB
- Find values with OR operator in MongoDB and format the result.?
- Query MongoDB with “like” implementation on name and email field beginning with a specific letter?
- Find the frequency of unique values for each column in an R data frame.
- Extract all the values and display in a single line with MongoDB
- Count the documents with a field value beginning with 13
- How to get unique values from MongoDB collection?
- Display the last two values from field with MongoDB
- MongoDB query to get record beginning with specific element from an array?
- Working with Aggregation to match all the values in MongoDB
- Select multiple values with MongoDB OR operator
- Fetch data between two dates and with a specific value in MongoDB. Group and get the sum with count?
Advertisements