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?

7
  • There is no order in objects, so the order shouldn't matter
    – adeneo
    Jun 25, 2015 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. Jun 25, 2015 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, 2015 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, 2015 at 16:00
  • possible duplicate of Does JavaScript Guarantee Object Property Order?
    – Jason Cust
    Jun 25, 2015 at 16:03

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.