1

Is there a way to retrieve my mongoose db info in the same order that it appears in the schema?

It seems to group by type when it returns the objects.

example:

//schema
var data = {
    element1 : String,
    element2 : Number,
    element3 : Array,
    element4 : Number,
}
//.find() returns
var data = {
    element1 : String,
    element2 : Number,
    element4 : Number,
    element3 : Array,
}

I need to write a file with the information in the specific order that it appears in the schema so if it is possible to retrieve it in that order it will save a lot of code.

If that isn't possible, is it possible to retrieve the schema to grab the keys in order so that I can match them up when writing the file?

BrentShanahan
  • 611
  • 1
  • 7
  • 15
  • There is no order in objects, so the order shouldn't matter – adeneo Jun 25 '15 at 15:44
  • I see, it looks like I have some code to write then... Out of curiosity, is it the asynchronous aspect of the retrieval that is causing the largest element (the array) to return as the last element when written? In other words, how is it consistently returning in the same order? It doesn't appear to be alphabetically based or any other pattern I can see. – BrentShanahan Jun 25 '15 at 15:56
  • The asynchronous aspect of it probably has nothing to do with it. Someone else may have a better reason, but the process can be deterministic, but that does not necessarily imply predictability in a practical sense. – Hyo Byun Jun 25 '15 at 15:59
  • 2
    It's completely dependent on the underlying host engine's implementation of the ECMA-Script spec. It can differ from implementation to implementation and by versions within that implementation. Object order is not guaranteed and should not be relied upon. If that is a requirement, use an Array object as that is guaranteed to maintain order across implementations. – Jason Cust Jun 25 '15 at 16:00
  • possible duplicate of [Does JavaScript Guarantee Object Property Order?](http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Jason Cust Jun 25 '15 at 16:03
  • In my experience, the properties are stored in MongoDB in the same order that they were originally added to the Javascript object. (Except for embedded documents, which are stored in [reverse order](https://github.com/Automattic/mongoose/issues/2749), unless `retainKeyOrder` is set.) – joeytwiddle Oct 03 '16 at 10:41
  • I have not found a way to retrieve the schema specification once the model has been created. However you can store your schema specification object in its own module, or expose it some other way before creating the model. – joeytwiddle Oct 03 '16 at 10:43

1 Answers1

0

You will have to use an array.

An object is a member of the type Object. It is an unordered collection of properties each of which contains a primitive value, object, or function. A function stored in a property of an object is called a method.

http://www.ecma-international.org/publications/files/ECMA-ST-ARCH/ECMA-262,%203rd%20edition,%20December%201999.pdf

Hyo Byun
  • 982
  • 6
  • 15