
- 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 do I order a list and add position to its items in MongoDB?
To order a list, use sort(). Let us create a collection with documents −
> db.demo581.insertOne({"Name":"Chris","Score":56});{ "acknowledged" : true, "insertedId" : ObjectId("5e91cbbbfd2d90c177b5bcb6") } > db.demo581.insertOne({"Name":"Bob","Score":240});{ "acknowledged" : true, "insertedId" : ObjectId("5e91cbbbfd2d90c177b5bcb7") } > db.demo581.insertOne({"Name":"David","Score":150});{ "acknowledged" : true, "insertedId" : ObjectId("5e91cbbcfd2d90c177b5bcb8") }
Display all documents from a collection with the help of find() method −
> db.demo581.find();
This will produce the following output −
{ "_id" : ObjectId("5e91cbbbfd2d90c177b5bcb6"), "Name" : "Chris", "Score" : 56 } { "_id" : ObjectId("5e91cbbbfd2d90c177b5bcb7"), "Name" : "Bob", "Score" : 240 } { "_id" : ObjectId("5e91cbbcfd2d90c177b5bcb8"), "Name" : "David", "Score" : 150 }
Following is the query to order a list and add position to its items in MongoDB −
> db.demo581.createIndex({Score:1}) { "createdCollectionAutomatically" : false, "numIndexesBefore" : 1, "numIndexesAfter" : 2, "ok" : 1 } > var i = 1; > db.demo581.find().sort({"Score": 1}).forEach(function (d){ ... d.Position = i; ... i++; ... db.demo581.save(d); ... })
Display all documents from a collection with the help of find() method −
> db.demo581.find();
This will produce the following output −
{ "_id" : ObjectId("5e91cbbbfd2d90c177b5bcb6"), "Name" : "Chris", "Score" : 56, "Position" : 1 } { "_id" : ObjectId("5e91cbbbfd2d90c177b5bcb7"), "Name" : "Bob", "Score" : 240, "Position" : 3 } { "_id" : ObjectId("5e91cbbcfd2d90c177b5bcb8"), "Name" : "David", "Score" : 150, "Position" : 2 }
- Related Articles
- How do I add a field to existing record in MongoDB?
- How do I add multiple items to a Java ArrayList in single statement?
- How do I copy items from list to list without foreach in C#?
- How to add items to a list in C#?
- How can I add items to a spinner in Android?
- How do I add a value to the top of an array in MongoDB?
- How do I insert an item between two items in a list in Java?
- How do I add an element to an array list in Java?
- How to Add Commas Between a List of Items Dynamically in JavaScript?
- How to Add Image into Dropdown List for each items?
- How do you remove multiple items from a list in Python?
- How do I query a MongoDB collection?
- How do you add an element to a list in Java?
- How do we add a menu list in HTML?
- How do I display a list of objects based on a specific property with MongoDB?

Advertisements