
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
How to use custom variable while updating a MongoDB document?
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" } { "_id" : ObjectId("5e94a072f5f1e70e134e269b"), "id" : 3, "Name" : "Sam" }
Following is the query to use custom variable while updating a MongoDB document −
> var replaceName="David"; > db.demo600.update({id:2},{$set:{Name:replaceName}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
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" : "David" } { "_id" : ObjectId("5e94a072f5f1e70e134e269b"), "id" : 3, "Name" : "Sam" }
- Related Articles
- Updating nested document in MongoDB
- How to use a select statement while updating in MySQL?
- Updating a MongoDB document and adding new keys only in the first document?
- Upsert in MongoDB while using custom _id values to insert a document if it does not exist?
- How do you update a MongoDB document while replacing the entire document?
- Conditional upsert (multiple insert) when updating document in MongoDB?
- MongoDB syntax for updating an object inside an array within a document?
- MongoDB query to increment a specific value using custom variable
- How to prevent MongoDB from returning the object ID while finding a document?
- Update array in MongoDB document by variable index?
- How to delete a document in MongoDB?
- In MongoDB how do you use $set to update a nested value/embedded document?
- How to add a sub-document to sub-document array in MongoDB?
- Updating data in MongoDB
- How to delete a MongoDB document using Java?

Advertisements