Mongo shell: how to display document's fields in lexicographic order?

By : rwg05
Source: Stackoverflow.com
Question!

This is what I get now:

> db.collection_name.findOne()
{
    "_id" : ObjectId("whatever")
    "c" : "a value"
    "a" : "a value"
    "b" : "a value"
}

Is there any function that I could use so that I can see the document's fields in lexicographic order (on a find() or findOne())? I would like to see:

> db.collection_name.findOne().something()
{
    "_id" : ObjectId("whatever")
    "a" : "a value"
    "b" : "a value"
    "c" : "a value"
}
By : rwg05


Answers

since a document in JS is an associative map

var d = db.collection_name.findOne()
var sorted = {}
var keys = Object.keys(d).sort()
for (i = 0; i < keys.length; i++) {   
    key = keys[i];   
    sorted[key] = d[key]; 
 }

, you can sort it's keys and copy the ordered fields into a new document (sorted in that case)

By : Ori Dar


Shouldn't this be sufficient? Just notice this will remove .denominator, and replace the inner text with desired value.

$('.medium-item-price').text('$1')

$('.medium-item-price').text('$1');
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
<div class="medium-item-price"><span class="denominator">$</span>699.99</div>

By : Adam Azad


You can do it like following.

$('.medium-item-price').contents().last()[0].textContent='100';
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="medium-item-price">
  <span class="denominator">$</span>
  699.99
</div>

By : Azim


This video can help you solving your question :)
By: admin