-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to keep the fields order in DB when creating a new doc? #6944
Comments
@flight9 if you add a type property to your mesaTrees schema path object like all of your other paths, the order is restored. Here is an example using your schema with the suggested change: 6944.js#!/usr/bin/env node
'use strict';
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
const conn = mongoose.connection;
const Schema = mongoose.Schema;
const articleSchema = new Schema({
'ug_guid': {
'type': 'String'
},
'assetTypeId': {
'type': Schema.Types.ObjectId,
'ref': 'assetTypes'
},
'mesa_key': {
'type': 'String'
},
'site_specific_name': {
'type': 'String'
},
'serial_number': {
'type': 'String'
},
'mesaTrees': {
type: {
'what': [
{
'ns1': {
'type': 'String'
},
'ns2': {
'type': 'String'
},
'what1': {
'type': 'String',
'required': true
},
'what2': {
'type': 'String'
},
'what3': {
'type': 'String'
},
'what4': {
'type': 'String'
},
'what5': {
'type': 'String'
},
'what6': {
'type': 'String'
},
'what7': {
'type': 'String'
},
'what8': {
'type': 'String'
}
}
],
'ent': [
{
'ent1': {
'type': 'String',
'required': true
},
'ent2': {
'type': 'String'
},
'ent3': {
'type': 'String'
}
}
],
'at': {
'type': 'String'
}
}
}
});
const Article = mongoose.model('Article', articleSchema);
let data = {
ug_guid: '6db93bc6-f0eb-4c31-b4db-4b90a185f927',
assetTypeId: '5a436c14fb18bba37f8384e3',
mesa_key: 'mesa_key2_1',
site_specific_name: 'B Test Site',
serial_number: '93425726762957156',
mesaTrees: {
what:
[{
ns1: 'namespace1',
ns2: 'namespace2',
what1: 'what1',
what2: 'what2',
what3: 'what3',
what4: 'what4',
what5: 'what5',
what6: 'what6',
what7: 'what7',
what8: 'what8',
_id: '5a436c14fb18bba37f8384e3'
}],
ent:
[{
ent1: 'ent1',
ent2: 'ent2',
ent3: 'ent3',
_id: '5a436c14fb18bba37f8384e3'
},
{
ent1: 'ent4',
ent2: 'ent5',
ent3: 'ent6',
_id: '5a436c14fb18bba37f8384e3'
}
],
at: 'at1'
}
};
var art = new Article(data);
async function run() {
await conn.dropDatabase();
await art.save();
let doc = await Article.collection.findOne({});
console.log(doc);
return conn.close();
}
run(); Output:
I'm not sure if this is a bug, I'll leave this open and we'll investigate. Thanks for reporting this! |
@lineus thanks |
@lineus be careful, your solution converts const articleSchema = new Schema({
'ug_guid': {
'type': 'String'
},
'assetTypeId': {
'type': Schema.Types.ObjectId,
'ref': 'assetTypes'
},
'mesa_key': {
'type': 'String'
},
'site_specific_name': {
'type': 'String'
},
'serial_number': {
'type': 'String'
},
'mesaTrees': new Schema({
'what': [
{
'ns1': {
'type': 'String'
}
}
]
});
}); That should work better. The issue here is that Mongoose's steps for creating a new document via
|
Do you want to request a feature or report a bug?
Maybe a bug. Not sure
What is the current behavior?
When u save a new doc, it's fields order can't be kept as defined in schema or its orignal order.
If the current behavior is a bug, please provide the steps to reproduce.
But I got the order in MongoDB like: (NOTE:
mesaTrees
is promoted to front.)What is the expected behavior?
Keep the fields order in DB as defined in schema or its orignal order.
Version
node.js: 8.9.3
mongoose: 5.2.10
MongoDB: 3.6.1
The text was updated successfully, but these errors were encountered: