0

So I have a problem with understanding of how Mongo .create and .findAndUpdate operation works. I have mongoose 5.4.2X and a model with schema which has lot of key:value pairs (without any nested objects) in the exact order (in the code below I use 1. 2. 3. etc to show you the right order) like this:

let schema = new mongoose.Schema({
    1.code: {
        type: String,
        required: true
    },
    2.realm: {
        type: String,
        required: true,
    },
    3.type: {
        type: String,
        required: true,
        enum: ['D', 'M'],
    },
    4.open: Number,
    5.open_size: Number,
    6.key: typeof value,..
    7...another one key: value like previous one,
    8.VaR_size: Number,
    9.date: {
        type: Date,
        default: Date.now,
        required: true
    }
});

and a class object which have absolutely the same properties in the same order like schema above.

When I form data for Mongo via const contract = new Class_name (data) and using console.log(contract) I have a necessary object with properties in the exact right order like:

Contract {1.code: XXX, 2.realm: YYY, 3.type: D, .... 8.VaR_size: 12, 9.date: 327438}

but when I'm trying to create/update document to the DB via findOneAndUpdate or (findByID) it writes in alphabetical order but not the necessary 1->9, for example:

_id:5ca3ed3f4a547d4e88ee55ec
1.code:"RVBD-02.J"
7.VaR:0.9
(not the 1->9)...:...
8.VaR_size:0.22
__v:0
5.avg:169921

The full code snippet for writing is:

        let test = await contracts.findOneAndUpdate(
            { 
                code: `${item_ticker}-${moment().subtract(1, 'day').format('DD.MMM')}`  //how to find
            }, 
            contract, //document for writinng and options below
            {
                upsert : true,
                new: true,
                setDefaultsOnInsert: true,
                runValidators: true,
                lean: true
            }
        ).exec();

Yes, I have read the mongoose docs and don't find any option param for solving my problem or probably some optional params are matter but there are no description for this.

It's not my first project, the funny thing is, that when I'm inserting tons-of-docs via .insertMany docs are inserted according to schema order (not alphabetical)

My only question is:

How do I fix it? Is there any option param or it's a part of findAnd.... operations? If there is not solution, what should I do if for me it's necessary the right ordering and check existence of document before inserting it?

Update #1 after some time I rephrase my search query for google and find a relevant question on SW here: MongoDB field order and document position change after update

AlexZeDim
  • 3,520
  • 2
  • 28
  • 64

1 Answers1

0

Guess I found the right answer according to the link that I post earlier. So yes, it's part of MongoDB:

MongoDB allocates space for a new document based on a certain padding factor. If your update increases the size of the document beyond the size originally allocated the document will be moved to the end of the collection. The same concept applies to fields in a document.

by @Bernie Hackett


But in this useful comment still no solution, right? Wrong. It seems that the only way to evade this situation is using additions optional params during Model.find stage. The same ordering using during project stage via .aggregate and it looks like this:

Model.find({query},{
   "field_one":1,
   "field_two":1,
   .............
   "field_last":1
});
AlexZeDim
  • 3,520
  • 2
  • 28
  • 64