- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Transaction lock in MongoDB?
Transaction support isn’t available in MongoDB 4.0. To get similar results, use findOneAndUpdate().
Let us create a collection with documents −
> db.demo404.insertOne({"FirstName":"John"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f8c38fac4d418a0178592") } > db.demo404.insertOne({"FirstName":"Robert"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f8c3cfac4d418a0178593") } > db.demo404.insertOne({"FirstName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e6f8c40fac4d418a0178594") }
Display all documents from a collection with the help of find() method −
> db.demo404.find();
This will produce the following output −
{ "_id" : ObjectId("5e6f8c38fac4d418a0178592"), "FirstName" : "John" } { "_id" : ObjectId("5e6f8c3cfac4d418a0178593"), "FirstName" : "Robert" } { "_id" : ObjectId("5e6f8c40fac4d418a0178594"), "FirstName" : "Mike" }
Following is the query to use findOneAndUpdate and set lock in MongoDB −
> result=db.demo404.findOneAndUpdate({"in_transaction": {"$exists": false}}, {"$set": {"in_transaction": true}});
This will produce the following output −
{ "_id" : ObjectId("5e6f8c38fac4d418a0178592"), "FirstName" : "John" }
- Related Articles
- Checking transaction list waiting for lock-in SAP HANA
- MongoDB transaction & indexes for duplicate values
- Object level lock vs Class level lock in Java?
- Differentiate between cash transaction and credit transaction
- Difference between Object level lock and Class level lock in Java
- Use ReaderWriter Lock in C#
- Definition of Transaction in Database
- What will happen to the current MySQL transaction if a START TRANSACTION command is executed in the middle of that current transaction?
- What is transaction processing? Explain the properties of the transaction(DBMS)
- Check if Caps Lock and Num Lock is on or off through Console in C#
- Which statement, other than START TRANSACTION, is used for starting a transaction?
- Defining user lock settings in SAP HANA
- How to lock multiple tables in MySQL?
- How to start a transaction in JDBC?
- How does a Lock work?

Advertisements