4

Nodejs app with Express and MongoDB. I have form to insert title, author and body. After submitting data I noticed that instead of title, author and body it looks like body, author and title. So, I wonder is this order important in MongoDB? And why it switch positions. Image shows how it should be and what i got.

Code fragments (mongoose schema, pug view, main):

// article schema
let articleSchema = new Schema({
    title: String,
    author: String,
    body: String
})

  label Title:
  input.form-control(name='title', type='text')
#form-group
  label Author:
  input.form-control(name='author', type='text')
#form-group
  label Body:
  textarea.form-control(name='body')

article.title = req.body.title
article.author = req.body.author
article.body = req.body.body

P.S. App working only confusing order in DB.

P.P.S. My question is similar to that, but not a duplicated, because, they talked about updating existing data in DB, while i'm talking about adding new data to DB. So it's obvious that after updating it will appear in the end. But when I want add new data, I thought it should look like I wrote in mongoose scheme. So, for now, I only know it's not so important, but I would like to know solution to fix this wrong order if one exists.

BlackB0ne
  • 115
  • 10

1 Answers1

0

So, I wonder is this order important in MongoDB?

In most situations, no, the ordering of fields in a document are not important. MongoDB stores data records as BSON documents. BSON is a binary representation of JSON documents (source). Javascript does not guarentee object property order.

However, there are situations in MongoDB where field order is important. When querying embedded documents, the embedded document must exactly match the document specified in the query. More information can be found here.

Adam Harrison
  • 2,915
  • 2
  • 14
  • 24